GetSNTP( cServer, ::nPort, ::lSync )
- Code: Select all Expand view
struct NtpServerResponse
{
int m_nLeapIndicator; //0: no warning
//1: last minute in day has 61 seconds
//2: last minute has 59 seconds
//3: clock not synchronized
int m_nStratum; //0: unspecified or unavailable
//1: primary reference (e.g., radio clock)
//2-15: secondary reference (via NTP or SNTP)
//16-255: reserved
CNtpTime m_OriginateTime; //Time when the request was sent from the client to the SNTP server
CNtpTime m_ReceiveTime; //Time when the request was received by the server
CNtpTime m_TransmitTime; //Time when the server sent the request back to the client
CNtpTime m_DestinationTime; //Time when the reply was received by the client
double m_RoundTripDelay; //Round trip time in seconds
double m_LocalClockOffset; //Local clock offset relative to the server
};
HB_FUNC( GETSNTP )
{
SOCKET m_hSocket = socket( AF_INET, SOCK_DGRAM, 0 );
SOCKADDR_IN sockAddr;
NtpBasicInfo nbi;
int nSendSize = sizeof( NtpBasicInfo );
NtpFullPacket nfp;
int nReceiveSize = sizeof( NtpFullPacket );
NtpServerResponse response;
ZeroMemory( &sockAddr, sizeof( sockAddr ) );
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons( ( u_short ) hb_parnl( 2 ) ); // nPort
sockAddr.sin_addr.s_addr = inet_addr( hb_parc( 1 ) ); // lpszAscii
if (sockAddr.sin_addr.s_addr == INADDR_NONE)
{
LPHOSTENT lphost = gethostbyname( hb_parc( 1 ) );
if (lphost != NULL)
sockAddr.sin_addr.s_addr = ( ( LPIN_ADDR ) lphost->h_addr )->s_addr;
}
connect( m_hSocket, &sockAddr, sizeof( sockAddr ) );
ZeroMemory( &nbi, nSendSize );
nbi.m_LiVnMode = 27; //Encoded representation which represents NTP Client Request & NTP version 3.0
nbi.m_TransmitTimestamp = CNtpTime_GetCurrentTime();
send( m_hSocket, (LPCSTR) &nbi, nSendSize, 0 );
ZeroMemory( &nfp, nReceiveSize );
recv( m_hSocket, ( LPSTR ) &nfp, nReceiveSize, 0 );
response.m_nStratum = nfp.m_Basic.m_Stratum;
response.m_nLeapIndicator = (nfp.m_Basic.m_LiVnMode & 0xC0) >> 6;
response.m_OriginateTime = nfp.m_Basic.m_OriginateTimestamp;
response.m_ReceiveTime = nfp.m_Basic.m_ReceiveTimestamp;
response.m_TransmitTime = nfp.m_Basic.m_TransmitTimestamp;
response.m_RoundTripDelay = (response.m_DestinationTime - response.m_OriginateTime) - (response.m_ReceiveTime - response.m_TransmitTime);
response.m_LocalClockOffset = ((response.m_ReceiveTime - response.m_OriginateTime) + (response.m_TransmitTime - response.m_DestinationTime)) / 2;
hb_retclen( ( char * ) &response, sizeof( response ) );
}