hai guys iam planning to develop sntp client and server ,here i am keeping i devloped code for sntp client and server with a loop back address
Code:
/**************************** sntpclient.c ************************************/ 
#include    
#include    
#include    
#include    
/* includes for socket delarations */
#include    
#include    
#include    
#include    

#define     SRV_IP_ADRS  "127.0.0.1" 
#define     SRV_UDP_PORT   123  


void errExit(char *str)
{
  puts(str);
  exit(0);
}
typedef struct
{
  unsigned char mode : 3;
  unsigned char vn : 3;
  unsigned char li : 2;
  unsigned char stratum;
  int poll;
  int precision;
  unsigned long root_delay;
  unsigned long root_dispersion;
  unsigned long reference_identifier;
  unsigned long reference_timestamp_secs;
  unsigned long originate_timestamp_secs;
  unsigned long receive_timestamp_secs;
  unsigned long transmit_timestamp_secs;
 
}sntp_packet;

int main()
{
  int                 sockFd;
  struct sockaddr_in  srvAdr,cliAdr;
  sntp_packet sntp_msg_header;
  sntp_packet rxmsg;
  int              n;

  if( (sockFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    errExit("Can't open datagram socket\n");
  
  memset(&srvAdr,0,sizeof(srvAdr));
  srvAdr.sin_family      = AF_INET;
  srvAdr.sin_addr.s_addr = inet_addr(SRV_IP_ADRS);
  srvAdr.sin_port        = htons(SRV_UDP_PORT);

  sntp_msg_header.mode=3;
  sntp_msg_header.vn  =4;
  sntp_msg_header.li  =0;
  sntp_msg_header.stratum=0;
  sntp_msg_header.poll=0;
  sntp_msg_header.precision=0;
  sntp_msg_header.root_delay=0;
  sntp_msg_header.root_dispersion=0;
  sntp_msg_header.reference_identifier=0;
  sntp_msg_header.reference_timestamp_secs=0;
  sntp_msg_header.originate_timestamp_secs=0;
  sntp_msg_header.receive_timestamp_secs=0;
  sntp_msg_header.transmit_timestamp_secs=time(NULL);

   n = sizeof(sntp_packet); 
  if(sendto(sockFd,&sntp_msg_header,n,0,(struct sockaddr*)&srvAdr,sizeof(srvAdr)) != n)
    errExit("sendto error\n");

  n = recv(sockFd,(sntp_packet*)&rxmsg,sizeof(rxmsg), 0);
  if(n < 0)
    errExit("recv error\n");

  printf("Received following message:\n%u \n%u \n%u \n%u \n%d \n%d \n%ld \n%ld \n%ld \n%ld \n%ld \n%ld \n%ld",rxmsg.mode,rxmsg.vn,rxmsg.li,rxmsg.stratum,rxmsg.poll,rxmsg.precision,  rxmsg.root_delay,rxmsg.root_dispersion,rxmsg.reference_identifier,rxmsg.reference_timestamp_secs,rxmsg.originate_timestamp_secs,rxmsg.receive_timestamp_secs,rxmsg.transmit_timestamp_secs); 
}
and server is
Code:
/**************************** sntp srver.c ************************************/ 
#include    
#include    
#include    
#include 
/* standard include for socket definitions */
#include    
#include    
#include    
#include    
#include    

#define     SRV_UDP_PORT    8000 


void errExit(char *str)
{
  char errorBuff[100];
  puts(str);
  perror(errorBuff);
  exit(0);
}
typedef struct
{
  unsigned char mode : 3;
  unsigned char vn : 3;
  unsigned char li : 2;
  unsigned char stratum;
  int poll;
  int precision;
  unsigned long root_delay;
  unsigned long root_dispersion;
  unsigned long reference_identifier;
  unsigned long reference_timestamp_secs;
  unsigned long originate_timestamp_secs;
  unsigned long receive_timestamp_secs;
  unsigned long transmit_timestamp_secs;
}sntp_packet;

int main()
{
  int                 sockFd;
  struct sockaddr_in  srvAdr, cliAdr;
  int                 cliLen,n;
  sntp_packet srv_header;
  sntp_packet srv;
  
  if( (sockFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    errExit("Can't open datagram socket\n");
  
  memset(&srvAdr,0,sizeof(srvAdr));
  srvAdr.sin_family      = AF_INET;
  srvAdr.sin_addr.s_addr = htonl(INADDR_ANY);
  srvAdr.sin_port        = htons(SRV_UDP_PORT);

  if(bind(sockFd,(struct sockaddr*)&srvAdr, sizeof(srvAdr)) < 0)
    errExit("Can't bind local address\n");

  printf("Server waiting for messages\n");
  while(1)
  {
    cliLen = sizeof(cliAdr);
    n = recvfrom(sockFd, (sntp_packet *)&srv, sizeof(sntp_packet), 0, (struct sockaddr *)&cliAdr, &cliLen);
    srv_header.mode=4;
    srv_header.li  =0;
    srv_header.vn  =srv.vn;
    srv_header.stratum=1;
    srv_header.poll=srv.poll;
    srv_header.precision=0;// -log2 server significant bits (srv->precision;)
    srv_header.root_delay=0;
    srv_header.root_dispersion=0;
    srv_header.reference_identifier=0;//ntp.pool.org();
    srv_header.reference_timestamp_secs=0;//ntp.pool.org();
    srv_header.originate_timestamp_secs=srv.originate_timestamp_secs;
    srv_header.receive_timestamp_secs=time(NULL);//"time of day";
    srv_header.transmit_timestamp_secs=time(NULL);//"time of day";
    
    if(n < 0)
      errExit("recvfrom error\n");

    if(sendto(sockFd,(sntp_packet*)&srv_header,n,0,(struct sockaddr *)&cliAdr,cliLen) != n)
      errExit("sendto error\n");

    printf("Received  messages:\n%u \n%u \n%u \n%u \n%d \n%d \n%ld \n%ld \n%ld \n%ld \n%ld \n%ld \n%ld\n",srv.mode,srv.vn,srv.li,srv.stratum,srv.poll,srv.precision,  srv.root_delay,srv.root_dispersion,srv.reference_identifier,srv.reference_timestamp_secs,srv.originate_timestamp_secs,srv.receive_timestamp_secs,srv.transmit_timestamp_secs);

    printf("Above  messages recieved  from client %s",
            inet_ntoa(cliAdr.sin_addr));
  }
}
here whats my doubt is what data client can put in originate time stamp and transmitt time stamp in order to send to to the sntp server ?
how to calculate precision, rootdelay, root_idspersion with respect to server?
how sntp server will contact reference identifier and which external source will it synchronized and how?
if sntp client put poll interval is 4 , how will client can poll the server 4 times?