国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Lua獲取網絡時間(獲取時間同步服務器的時間)

2024-07-21 23:04:31
字體:
來源:轉載
供稿:網友

網絡授時服務是一些網絡上的時間服務器提供的時間,一般用于本地時鐘同步。 授時服務有很多種,一般我們選擇RFC-868。這個協議的工作流程是:(S代表Server,C代表Client)

S: 檢測端口37
U: 連接到端口37
S: 以32位二進制數發送時間
U: 接收時間
U: 關閉連接
S: 關閉連接
協議非常簡單,用TCP連接上后,服務器直接把時間發送回來。發送的是從1900年1月1日午夜到現在的秒數。

使用luasocket
實現的方案有很多種,Lua不一定是最簡單的,選擇只是出于個人興趣。直接上代碼吧

------------------------------------------------------------------------------- Network Time Protocal-- Author: ani_di-----------------------------------------------------------------------------package.cpath = package.cpath .. ';D://tools//Lua//5.1//clibs//?.dll;?.dll'local socket = require "socket.core"server_ip = {    -- "129.6.15.29",    "132.163.4.101",    "132.163.4.102",    "132.163.4.103",    "128.138.140.44",    "192.43.244.18",    "131.107.1.10",    "66.243.43.21",    "216.200.93.8",    "208.184.49.9",    "207.126.98.204",    "207.200.81.113",    "205.188.185.33"}function nstol(str)  assert(str and #str == 4)  local t = {str:byte(1,-1)}  local n = 0  for k = 1, #t do    n= n*256 + t[k]  end  return nend-- get time from a ip address, use tcp protoclfunction gettime(ip)  print('connect ', ip)  local tcp = socket.tcp()  tcp:settimeout(10)  tcp:connect(ip, 37)  success, time = pcall(nstol, tcp:receive(4))  tcp:close()  return success and time or nilendfunction nettime()  for _, ip in pairs(server_ip) do    time = gettime(ip)    if time then       return time    end  endend


代碼原理不細說,非常簡單。唯一值得一提的是socket庫包含。最開始用的這句 require "socket"

在解釋器中表現很好,但在用C中調用會找不到相應的module。錯誤提示

  no field package.preload['socket']  no file './socket.lua'  no file 'F:/Projects/Lua/nettime/lua/socket.lua'  no file 'F:/Projects/Lua/nettime/lua/socket/init.lua'  no file 'F:/Projects/Lua/nettime/socket.lua'  no file 'F:/Projects/Lua/nettime/socket/init.lua'  no file 'D:/tools/Lua/5.1/lua/socket.luac'  no file './socket.dll'  no file './socket51.dll'  no file 'F:/Projects/Lua/nettime/socket.dll'  no file 'F:/Projects/Lua/nettime/socket51.dll'  no file 'F:/Projects/Lua/nettime/clibs/socket.dll'  no file 'F:/Projects/Lua/nettime/clibs/socket51.dll'  no file 'F:/Projects/Lua/nettime/loadall.dll'  no file 'F:/Projects/Lua/nettime/clibs/loadall.dll'.


網上也有好多類似的提問,大抵是沒仔細看作者的Guide。顯著的有這么一句

The other two environment variables instruct the compatibility module to look for dynamic libraries and extension modules in the appropriate directories and with the appropriate filename extensions.>

LUAPATH=/?.lua;?.lua LUACPATH=/?.dll;?.dll

至于"socket.core",windows默認安裝位于“/socket/core.dll”。

C宿主調用

#include <stdio.h>#include <string.h>#include <lua.h>#include <lauxlib.h>#include <lualib.h>#include <time.h>#include <Windows.h>int load(lua_State* L, const char* func, unsigned int* utc) {  lua_getglobal(L, func);  if (lua_pcall(L, 0, 1, 0)) {    printf("Error Msg pcall %s./n", lua_tostring(L, -1));    return -1;  }  if (!lua_isnumber(L,-1)) {    printf("time should be a number/n" );    return -2;  }  *utc = lua_tonumber(L,-1);  lua_pop(L, -1);  return 0;}void TimetToFileTime( time_t t, LPFILETIME pft ){  LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000;  pft->dwLowDateTime = (DWORD) ll;  pft->dwHighDateTime = ll >>32;}int main(){  lua_State* L = luaL_newstate();  unsigned int utc = 0;  luaL_openlibs(L); if (luaL_loadfile(L, "nettime.lua") || lua_pcall(L, 0, 0, 0)) {    printf("Error Msg load %s./n", lua_tostring(L, -1));    return -1;  }  do {  if(load(L,"nettime", &utc) == 0) {    time_t tt = utc - 2208988800L;    SYSTEMTIME st;    FILETIME ft;    TimetToFileTime(tt, &ft);    if (FileTimeToSystemTime(&ft, &st))    {      printf("Today is: %d-%d-%d/n", st.wYear, st.wMonth, st.wDay);      SetSystemTime(&st);    }    break;  } else {    puts("No network!");    Sleep(10000);  }  } while (1);  lua_close(L);  return 0;}

 

 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彭山县| 斗六市| 泸水县| 佛山市| 新宾| 霍山县| 林周县| 周口市| 高要市| 临洮县| 信宜市| 平顶山市| 察隅县| 库伦旗| 廊坊市| 收藏| 商丘市| 桐城市| 西吉县| 丹江口市| 松溪县| 长沙市| 兴业县| 本溪| 桂平市| 平江县| 萍乡市| 互助| 房产| 霍林郭勒市| 阿克苏市| 西华县| 宜宾市| 绍兴县| 尉犁县| 松桃| 海门市| 寿宁县| 措美县| 临夏县| 眉山市|