Network Synchronous Time Client Code (RFC868C ++Win32Socket)

xiaoxiao2021-03-06  104

A large number of industrial machines must need to synchronize school, so I will look at RFC868. I will try to obtain a small program for network time. You can learn from your own synchronous calibration procedure, but the program structure is not rigorous, For a while, I will post the code of the server segment, today release the client: #pragma Warning (Disable: 4530) #pragma Warning (Disable: 4786)

#include #include #include using namespace std; #include #include

static char * get_error_msg () {static char msg_buf [1024]; FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError (), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language msg_buf, 1023, NULL); return msg_buf;}

Void Time_Internet_to_local (Void * RECV_BUF) // received data conversion to local time {assert (recv_buf); char P [4], * precv = ((char *) RECV_BUF) 3; for (int i = 0; i <4; i ) p [i] = * precv--; // network host data for conversion * (long *) p - = 2208988800; // Convert to local time Time_t

SystemTime St; Tm * PTM = GMTIME ((Time_t *) P); St.wyear = PTM-> TM_Year 1900; St.WMONTH = PTM-> TM_MON 1; St.wday = PTM-> TM_MDAY; St.Whour = PTM-> TM_HOUR; St.Wminute = PTM-> TM_MIN; st.wsecond = PTM-> TM_SEC; st.wmilliseconds = 0; setsystemTime (& st);}

Void print_time (void * recv_buf) // Print received data {assert (Recv_buf); char P [4], * precv = ((char *) RECV_BUF) 3; for (int i = 0; i <4; i ) // network host data for converting P [I] = * precv--; * (long *) p - = 2208988800; char buf [20]; strftime (buf, 20, "% y-% m-% D% h:% m:% s ", localtime ((Time_t *) P)); cout << buf << endl;}

Void TCP (Void * IP) // RFC868 Port 37 TCP Protocol {struct sockaddr_in server; server.sin_family = AF_INET; Server.sin_Port = HTONS (37); server.sin_addr.s_addr = inet_addr ((char *) IP);

Socket S = Socket (AF_INET, SOCK_STREAM, 0); if (s == invalid_socket) cout << __line __ << "Socket Error:" << Wsagetlasterror () << Endl; int RET = Connect (S, (Struct SockAddr * ) & server, sizeof (server); if (ret == socket_error) cout << __line__ << GET_ERROR_MSG () << Endl; Else {char XX [4]; RET = RECV (S, XX, 4, 0); IF (RET == SOCKET_ERROR) COUT << __line__ << GET_ERROR_MSG () << endl; else {print_time (xx); // time_internet_to_local (xx); set local time is best added to add a judgment network delay}

IF (Shutdown (S, SD_RECEIVE) == Socket_ERROR) COUT << __line__ << GET_ERROR_MSG () << Endl;} CloseSocket (s);}

Void UDP (Void * IP) // RFC868 Port 37 UDP {structure sockaddr_in server; server.sin_family = AF_INET; server.sin_port = htons (37); server.sin_addr.s_addr = inet_addr ((char *) IP); Socket S = Socket (AF_INET, SOCK_DGRAM, 0); if (s == invalid_socket) {cout << __line__ << get_error_msg () << end1;}

INT RET, C_LEN = SizeOf (Server); char r_buf [4]; RET = Sendto (S, R_BUF, 0, 0, (Struct SockAddr *) & Server, SIZEOF (Server)); // Send an empty datagram ing ( RET == SOCKET_ERROR) COUT << __line__ << get_error_msg () << endl;

FD_set = {3, 0}; //tv.tv_sec = 0, TV.tv_usec = 100000; 0.1s fd_Sero (& Read_set); fd_set (s, & read_set); return = SELECT (0, & read_set, null, NULL, & TV); if (RET == SOCKET_ERROR) {cout << __line__ << get_error_msg () << end1;} else if (fd_isset (s, & read_set) {RET = Recvfrom (s, r_buf, 4, 0, (Struct SockAddr *) & Server, & C_LEN); if (RET == Socket_error) cout << __line__ << GET_ERROR_MSG () << Endl; Else {Print_time (r_buf);}}} int main (int Argc);} int CHAR * argv []) {Word WVersionRequested; Wsadata Wsadata; wvelSionRequested = MakeWord (2, 2); WSAStartup (WVersionRequested, & WSADATA);

Char * ipcn = "159.226.154.16"; // National Time Center Char * iPhk = "210.0.235.14"; // Hong Kong Observatory Time Time Center

Time_t Now; char buf [20] = {0};

Now = time (null); strftime (buf, 20, "% Y-% M-% D% H:% M:% S", localtime (& now)); COUT << "TCP Connection National Time Center" << ENDL << "Local Time" << BUF << Endl << "Remote Time"; TCP (IPCN);

Now = time (null); strftime (buf, 20, "% Y-% M-% D% H:% M:% S", localtime (& now)); COUT << "UDP connection Hong Kong Time Timing Center" <

WSACleanup (); return 0;} RFC 868 Network Working Group J. Postel - ISIRequest for Comments:. 868 K. Harrenstien - SRI May 1983 Time ProtocolThis RFC specifies a standard for the ARPA Internet community Hosts onthe ARPA Internet that choose to implement a Time Protocol are expectedto adopt and implement this standard.This protocol provides a site-independent, machine readable date andtime. The Time service sends back to the originating source the time inseconds since midnight on January first 1900.One motivation arises from the fact that not All Systems Have Adate / Time Clock, And All Are Subject To Occasional Human or MachineError. The Use of Time-Servers Makes It Possible To Quickly Confirm OrcorRect A System '

s idea of ​​the time, by making a brief poll of severalindependent sites on the network.This protocol may be used either above the Transmission Control Protocol (TCP) or above the User Datagram Protocol (UDP) .When used via TCP the time service works As Follows: S: Listen on Port 37 (45 Octal). U: Connect To Port 37. S: Send The Time AS A 32 Bit Binary Number. u: Receive The Time. u: Close The Connection. s: Close The Connection. . The server listens for a connection on port 37. When the connection is established, the server returns a 32-bit time value and closes the connection. If the server is unable to determine the time at its site, it should either refuse the connection Or Close It Without Sending Anything.RFC 868 May 1983Time Protocol WHEN USED VIA UDP The Time Service Works As Follows: S: Listen on Port 37 (45 Octal). u: Send An Empty DataGram T o port 37. S: Receive the empty datagram S:. Send a datagram containing the time as a 32 bit binary number U:. Receive the time datagram The server listens for a datagram on port 37. When a datagram arrives, the server. returns a datagram containing the 32-bit time value. If the server is unable to determine the time at its site, it should discard the arriving datagram and make no reply.The TimeThe time is the number of seconds since 00:00 (midnight) 1 January 1900GMT, Such That The Time 1 IS 12:00:01 AM on 1 January 1900 GMT;

转载请注明原文地址:https://www.9cbs.com/read-97266.html

New Post(0)