Advertisement
Guest User

Client/Server uni-directional latency test

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