// Make the procedure into Block
// 2014.7.23 OK
//
# Include "csocket. H"
# Define ntp_server_ip "192.168.1.12"/* NTP service IP */
Int sendpkt (INT sockfd, struct addrinfo * res)
{
Struct ntp_control ntppack, newpack;
// Put the date into the ntppack
Ntppack. li_vn_mode = 0x26;
Ntppack. r_m_e_op = 0x03;
Ntppack. Sequence = 0x01;
Ntppack. Status = 0x00;
Ntppack. associd = 0x00;
Ntppack. offset = 0x00;
Ntppack. Count = 0x00;
Int I;
For (I = 0; I <1; I ++)
{
Int ret = sendto (sockfd, & ntppack, sizeof (ntppack), 0, res-> ai_addr, res-> ai_addrlen );
If (Ret <0 ){
Perror ("sendto ");
Return 1;
}
}
}
Int getresponse (INT sockfd, struct addrinfo * res)
{
Struct ntp_control rpkt;
Fd_set pending_data;
Struct timeval block_time;
Char * refID;
/* Call the select () function and set the timeout value to 1 S */
Fd_zero (& pending_data );
Fd_set (sockfd, & pending_data );
Block_time. TV _sec = 10; // how time to ask
Block_time. TV _usec = 0;
If (select (sockfd + 1, & pending_data, null, null, & block_time)> 0)
{
Int num;
/* Receive server information */
If (num = recvfrom (sockfd, & rpkt,
Sizeof (rpkt), 0, res-> ai_addr, & res-> ai_addrlen) <0)
{
Perror ("recvfrom ");
Return 0;
}
/* Set the data structure for receiving NTP packets */
Int mode = pkt_mode (rpkt. li_vn_mode );
Int version = pkt_version (rpkt. li_vn_mode );
Int leap = pkt_leap (rpkt. li_vn_mode );
Int response = ctl_isresponse (rpkt. r_m_e_op );
Int error = ctl_iserror (rpkt. r_m_e_op );
Int more = ctl_ismore (rpkt. r_m_e_op );
Int readvar = ctl_op (rpkt. r_m_e_op );
Int sequence = ntohs (rpkt. Sequence );
Int status = ntohs (rpkt. status );
Int associd = ntohs (rpkt. associd );
Int offset = ntohs (rpkt. offset );
Int COUNT = ntohs (rpkt. Count );
Printf ("Li = % d, version = % d, mode = % d \ n", leap, version, mode );
Printf ("response = % d, error = % d, more = % d, readvar = % d \ n", response, error, more, readvar );
Printf ("sequence = % d \ n", sequence );
Printf ("status = % d, associd = % d \ n", status, associd );
Printf ("offset = % d, Count = % d \ n", offset, count );
Printf ("################ data #################### \ n ");
Char * version_1 = strtok (rpkt. Data ,",");
Char * processor = strtok (null ,",");
Char * system = strtok (null ,",");
Char * leap_1 = strtok (null ,",");
Char * stratum = strtok (null ,",");
Char * precision = strtok (null ,",");
Char * rootdelay = strtok (null ,",");
Char * rootdispersion = strtok (null ,",");
Char * peer = strtok (null ,",");
Char * refID = strtok (null ,",");
Char * reftime = strtok (null ,",");
Char * poll = strtok (null ,",");
Char * Clock = strtok (null ,",");
Char * State = strtok (null ,",");
Char * offset1 = strtok (null ,",");
Char * frequency = strtok (null ,",");
Char * jitter = strtok (null ,",");
Char * Stability = strtok (null ,",");
// Printf ("refID: --- % s ---- \ n", refID );
}
If (strcmp (refID + 6, "GPS") = 0)
Printf ("OK \ n ");
Return 1;
}
Int main ()
{
Struct addrinfo * res = NULL, hints;
Memset (& hints, 0, sizeof (hints ));
Hints. ai_family = af_unspec;
Hints. ai_socktype = sock_dgram;
Hints. ai_protocol = ipproto_udp;
/* Call the getaddrinfo () function to obtain the address information */
Int rc = getaddrinfo (ntp_server_ip, ntp_port_str, & hints, & res );
If (RC! = 0)
{
Perror ("getaddrinfo ");
Return 1;
}
Int FD = socket (res-> ai_family, res-> ai_socktype, res-> ai_protocol );
If (FD <0 ){
Perror ("socket ");
Return 0;
}
Sendpkt (FD, Res );
Int I;
I = getresponse (FD, Res );
Printf ("nihao = % d", I );
Close (FD );
}
Http://www.gpstime.com.cn/