The Network Time service is a time server provided by some network, which is usually used for local clock synchronization. There are many kinds of timing service, in general we choose RFC-868. The workflow of this Protocol is: (S represents server,c on behalf of client)
S: Detection Port 37
U: Connect to Port 37
S: Send time in 32-bit binary number
U: Receive Time
U: Close Connection
S: Close the connection
Protocol is very simple, with TCP connection, the server directly send back the time. Sent is the number of seconds from midnight January 1, 1900 to now.
Using Luasocket
There are many scenarios to implement, LUA is not necessarily the simplest, and the choice is only for personal interest. Just go to the code.
-------------------------------------------------------------------------------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 a nd #str = = 4) Local t = {str:byte (1,-1)} local n = 0 for k = 1, #t does n= n*256 + t[k] End return N--G Et time from IP addresses, use TCP PROTOCL function gettime (IP) print (' Connect ', IP) Local TCP = SOCKET.TCP () tcp:s Ettimeout (+) tcp:connect (IP, Panax) success, time = Pcall (Nstol, Tcp:receive (4)) Tcp:close () return success and TIME or nil End Function NetTime () to _, IP in pairs (server_ip) Does time = gettime (IP) if time then retur
N Time End End
The code principle is not elaborate, very simple. The only thing worth mentioning is that the socket library contains. The first use of this sentence require "socket"
It behaves well in the interpreter, but the corresponding module cannot be found in the C call. Error tips
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 '.
There are a lot of similar questions on the Internet, probably not carefully read the author's Guide. Obviously, there's this one sentence.
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
As for "Socket.core", the Windows default installation is located in "\socket\core.dll".
C Host Calls
#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* ut
c) {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;
}