Advertisement
Guest User

.c

a guest
May 28th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. /*
  2. * udpserver.c - A simple UDP echo server
  3. * usage: udpserver <port>
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <netdb.h>
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #include <arpa/inet.h>
  15. #include <stdint.h>
  16.  
  17. #include <time.h>
  18.  
  19. #define BUFSIZE 1024 * 4
  20.  
  21. /*
  22. * error - wrapper for perror
  23. */
  24. void error(char *msg) {
  25. perror(msg);
  26. exit(1);
  27. }
  28.  
  29. int main(int argc, char **argv) {
  30. int sockfd; /* socket */
  31. int portno; /* port to listen on */
  32. int clientlen; /* byte size of client's address */
  33. struct sockaddr_in serveraddr; /* server's addr */
  34. struct sockaddr_in clientaddr; /* client addr */
  35. struct hostent *hostp; /* client host info */
  36. char buf[BUFSIZE]; /* message buf */
  37. char *hostaddrp; /* dotted decimal host addr string */
  38. int optval; /* flag value for setsockopt */
  39. ssize_t n; /* message byte size */
  40. int16_t second;
  41.  
  42. time_t segundos, seg_old;
  43.  
  44. /*
  45. * check command line arguments
  46. */
  47. if (argc != 2) {
  48. fprintf(stderr, "usage: %s <port>\n", argv[0]);
  49. exit(1);
  50. }
  51. portno = atoi(argv[1]);
  52.  
  53. /*
  54. * socket: create the parent socket
  55. */
  56. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  57. if (sockfd < 0)
  58. error("ERROR opening socket");
  59.  
  60. /* setsockopt: Handy debugging trick that lets
  61. * us rerun the server immediately after we kill it;
  62. * otherwise we have to wait about 20 secs.
  63. * Eliminates "ERROR on binding: Address already in use" error.
  64. */
  65. optval = 1;
  66. setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
  67. (const void *)&optval , sizeof(int));
  68.  
  69. /*
  70. * build the server's Internet address
  71. */
  72. bzero((char *) &serveraddr, sizeof(serveraddr));
  73. serveraddr.sin_family = AF_INET;
  74. serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
  75. serveraddr.sin_port = htons((unsigned short)portno);
  76.  
  77. /*
  78. * bind: associate the parent socket with a port
  79. */
  80. if (bind(sockfd, (struct sockaddr *) &serveraddr,
  81. sizeof(serveraddr)) < 0)
  82. error("ERROR on binding");
  83.  
  84.  
  85. /*
  86. * main loop: wait for a datagram, then echo it
  87. */
  88. clientlen = sizeof(clientaddr);
  89.  
  90. segundos = time(NULL);
  91. int counter = 0;
  92. int bytes = 0;
  93.  
  94. while (1) {
  95.  
  96. /*
  97. * recvfrom: receive a UDP datagram from a client
  98. */
  99. bzero(buf, BUFSIZE);
  100. n = recvfrom(sockfd, buf, BUFSIZE, 0,
  101. (struct sockaddr *) &clientaddr, &clientlen);
  102.  
  103. counter ++;
  104. int seg_new = time(NULL);
  105. if( segundos != seg_new )
  106. {
  107. printf( "Segundos %d counter %d bytes %d\n", segundos, counter, bytes );
  108. segundos = seg_new; counter = 0; bytes = 0;
  109. }
  110.  
  111.  
  112. if (n < 0)
  113. error("ERROR in recvfrom");
  114.  
  115. /*
  116. * gethostbyaddr: determine who sent the datagram
  117. hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
  118. sizeof(clientaddr.sin_addr.s_addr), AF_INET);
  119. if (hostp == NULL)
  120. error("ERROR on gethostbyaddr");
  121. hostaddrp = inet_ntoa(clientaddr.sin_addr);
  122. if (hostaddrp == NULL)
  123. error("ERROR on inet_ntoa\n");
  124. */
  125. /* printf("server received datagram from %s (%s)\n",
  126. hostp->h_name, hostaddrp);
  127. */
  128. bytes += n;
  129.  
  130. /*
  131. * sendto: echo the input back to the client
  132. */
  133. // n = sendto(sockfd, buf, strlen(buf), 0,
  134. // (struct sockaddr *) &clientaddr, clientlen);
  135. // n = sendto( sockfd, "HOLA!!!!", strlen("HOLA!!!!") + 1, 0,
  136. // (struct sockaddr *) &clientaddr, clientlen);
  137. // if (n < 0)
  138. // error("ERROR in sendto");
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement