Advertisement
Guest User

Untitled

a guest
May 24th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. /************************************************************************/
  2. /* Include Section */
  3. /************************************************************************/
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <netinet/tcp.h>
  13. #include <arpa/inet.h>
  14. #include <sys/time.h>
  15. /************************************************************************/
  16. /* Variables declaration Section */
  17. /************************************************************************/
  18. #define MAX_SAMPL 256
  19.  
  20. static time_t starttime;
  21. /************************************************************************/
  22. /* Code Section */
  23. /************************************************************************/
  24.  
  25. static int moxaconnect(char *ipaddr, unsigned short port)
  26. {
  27. int sockfd;
  28. int error;
  29. int flag = 1;
  30. struct sockaddr_in addr_dest;
  31.  
  32. addr_dest.sin_family = AF_INET;
  33. addr_dest.sin_addr.s_addr = inet_addr(ipaddr);
  34. addr_dest.sin_port = htons(port);
  35.  
  36. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  37. if (sockfd<0) {
  38. return(-2);
  39. }
  40.  
  41. error = connect(sockfd, (struct sockaddr *)&addr_dest, sizeof(addr_dest));
  42. if (error) {
  43. close(sockfd);
  44. return(error);
  45. }
  46. error = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
  47. if (error) {
  48. close(sockfd);
  49. return error;
  50. }
  51. return sockfd;
  52. }
  53.  
  54. static void get_usec_now(unsigned int *usec)
  55. {
  56. struct timeval tv;
  57. gettimeofday(&tv, NULL);
  58.  
  59. tv.tv_sec -= starttime;
  60. *usec=(tv.tv_sec*1000000) + tv.tv_usec;
  61. }
  62.  
  63. int main(int argc, char **argv)
  64. {
  65. int sock, error, i;
  66. unsigned char val = 'a';
  67. unsigned char bytein;
  68. unsigned int cur_diff;
  69. unsigned int sent_usec;
  70. unsigned int recv_usec;
  71. unsigned int min_diff = 0xffffffff;
  72. unsigned int max_diff = 0;
  73. unsigned int sum_diff = 0;
  74. struct timeval tv;
  75.  
  76. if (argc<3) {
  77. printf("Usage: %s <moxa ip address> <port>\n", argv[0]);
  78. return -1;
  79. }
  80. sock=moxaconnect(argv[1], atoi(argv[2]));
  81. if (sock<0) {
  82. printf("Error %d opening socket\n", sock);
  83. return (sock);
  84. }
  85.  
  86. gettimeofday(&tv, NULL);
  87. starttime = tv.tv_sec;
  88.  
  89. printf("Start sending and receving %d bytes - All times in microseconds\n", MAX_SAMPL);
  90.  
  91. for(i=0; i<MAX_SAMPL; i++) {
  92. error = send(sock, &val, 1, 0);
  93. get_usec_now(&sent_usec);
  94. if (error != 1)
  95. printf("Error %d writing on socket %d\n", error, sock);
  96.  
  97.  
  98. error = read(sock, &bytein, 1);
  99. get_usec_now(&recv_usec);
  100. if (error != 1)
  101. printf("Error %d reading on socket %d\n", error, sock);
  102.  
  103. cur_diff = recv_usec - sent_usec;
  104. max_diff = (cur_diff > max_diff) ? cur_diff : max_diff;
  105. min_diff = (cur_diff < min_diff) ? cur_diff : min_diff;
  106. sum_diff += cur_diff;
  107.  
  108. printf("%3d> SEND: %d\tRECV: %d\tDIFF: %d\tMIN: %d\tMAX: %d\tAVG: %d\n",
  109. i+1, sent_usec, recv_usec, cur_diff, min_diff, max_diff, ((sum_diff)/(i+1)));
  110. }
  111.  
  112. error = shutdown(sock, SHUT_RDWR);
  113. return error;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement