Advertisement
Guest User

Symmetria

a guest
Sep 21st, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Some notes: Boxes MUST be in the same time zone and MUST be NTP synched
  2.  
  3. Client.c
  4.  
  5. #include <arpa/inet.h>
  6. #include <netinet/in.h>
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <sys/timeb.h>
  14.  
  15. #define SRV_IP "CHANGE_ME"
  16. #define PORT 7890
  17. #define NUM_PACKETS 100
  18. #define PACKET_DELAY 100
  19.  
  20. void diep(char *stringz)
  21. {
  22.    perror(stringz);
  23.    exit(-1);
  24. }
  25.  
  26. struct s_timestamp {
  27.         int sequence;
  28.         int tseconds;
  29.         int tmilli;
  30. };
  31.  
  32. void delay_time(int PACKETDELAY) {
  33.    int t_diff = 0;
  34.    struct timeb t_delay_s, t_delay_e;
  35.  
  36.    memset(&t_delay_s, sizeof(struct timeb), 0);
  37.    memset(&t_delay_e, sizeof(struct timeb), 0);
  38.    ftime(&t_delay_s);
  39.    while(t_diff < PACKETDELAY) {
  40.      ftime(&t_delay_e);
  41.      t_diff=(1000 * (t_delay_e.time - t_delay_s.time) + (t_delay_e.millitm - t_delay_s.millitm));
  42.    }
  43. }
  44.  
  45. int main() {
  46.    struct sockaddr_in udp_socket;
  47.    char payload[sizeof(struct timeb)];
  48.    int u_sock, counter, socklen=sizeof(udp_socket);
  49.    struct s_timestamp p_data;
  50.    struct timeb t_current;
  51.  
  52.    if((u_sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
  53.         diep("socket");
  54.  
  55.    memset((char *)&udp_socket, 0, sizeof(udp_socket));
  56.  
  57.    udp_socket.sin_family = AF_INET;
  58.    udp_socket.sin_port = htons(PORT);
  59.  
  60.     if (inet_aton(SRV_IP, &udp_socket.sin_addr)==0) {
  61.        fprintf(stderr, "inet_aton() failed\n");
  62.        exit(1);
  63.    }
  64.  
  65.    for(counter = 0; counter < NUM_PACKETS; counter++) {
  66.       memset(&p_data, sizeof(struct s_timestamp),0);
  67.       printf("Sending packet %d\n", counter);
  68.  
  69.       ftime(&t_current);
  70.       p_data.tseconds = t_current.time;
  71.       p_data.tmilli = t_current.millitm;
  72.       p_data.sequence=counter;
  73.       printf("Transmitting packet with sequence number %d\n",p_data.sequence);
  74.       if (sendto(u_sock, (void *)&p_data, sizeof(struct s_timestamp), 0, (struct sockaddr *)&udp_socket, socklen) == -1)
  75.         diep("sendto()");
  76.       delay_time(PACKET_DELAY);
  77.       }
  78.    printf("Size of struct s_timestamp is %d\n",sizeof(struct s_timestamp));
  79. }
  80.  
  81.  
  82. Server.C:
  83.  
  84.  
  85. #include <arpa/inet.h>
  86. #include <netinet/in.h>
  87. #include <stdio.h>
  88. #include <sys/types.h>
  89. #include <sys/socket.h>
  90. #include <unistd.h>
  91. #include <string.h>
  92. #include <stdlib.h>
  93. #include <sys/timeb.h>
  94.  
  95. #define PORT 7890
  96. #define NUM_PACKETS 100
  97.  
  98. void diep(char *stringz)
  99. {
  100.    perror(stringz);
  101.    exit(-1);
  102. }
  103.  
  104. struct s_timestamp {
  105.         int sequence;
  106.         int tseconds;
  107.         int tmilli;
  108. };
  109.  
  110. int main() {
  111.    struct sockaddr_in udp_local, udp_socket;
  112.    int u_sock, counter, socklen=sizeof(udp_socket);
  113.    struct s_timestamp p_data;
  114.    struct timeb t_current;
  115.    int delay;
  116.  
  117.    if((u_sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
  118.         diep("socket");
  119.  
  120.    memset((char *)&udp_socket, 0, sizeof(udp_socket));
  121.  
  122.    udp_local.sin_family=AF_INET;
  123.    udp_local.sin_port=htons(PORT);
  124.    udp_local.sin_addr.s_addr = htonl(INADDR_ANY);
  125.    if(bind(u_sock, (struct sockaddr *)&udp_local, sizeof(udp_local)) == -1)
  126.        diep("socket");
  127.  
  128.    while(1) {
  129.    for(counter = 0; counter < NUM_PACKETS; counter++) {
  130.       if(recvfrom(u_sock, (void *)&p_data, sizeof(struct s_timestamp), 0, (struct sockaddr *)&udp_socket, &socklen) == -1)
  131.          diep("recvfrom()");
  132.       printf("Received packet with %d bytes of data with sequence number %d\n",socklen, p_data.sequence);
  133.       printf("Received timestamp %d:%d\n",p_data.tseconds, p_data.tmilli);
  134.       ftime(&t_current);
  135.       delay = (1000 * (t_current.time - p_data.tseconds) + (t_current.millitm - p_data.tmilli));
  136.       printf("Received packet.  Packet delay calculated at %d ms\n", delay);
  137.    }
  138.    }
  139.  
  140.    close(u_sock);
  141.    return 0;
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement