Guest User

Untitled

a guest
Dec 18th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. /* UDP echo server program -- udp-echo-server.c */
  2.  
  3. #include <stdio.h> /* standard C i/o facilities */
  4. #include <stdlib.h> /* needed for atoi() */
  5. #include <sys/types.h> /* system data type definitions */
  6. #if defined(_WIN32)
  7. #include <WinSock2.h>
  8. #pragma comment(lib, "Ws2_32.lib")
  9. #else
  10. #include <unistd.h> /* defines STDIN_FILENO, system calls,etc */
  11. #include <sys/socket.h> /* socket specific definitions */
  12. #include <netinet/in.h> /* INET constants and stuff */
  13. #include <arpa/inet.h> /* IP address conversion stuff */
  14. #include <netdb.h> /* gethostbyname */
  15. #endif // defined(_WIN32)
  16.  
  17. /* this routine echos any messages (UDP datagrams) received */
  18.  
  19. #define MAXBUF 1024*1024
  20.  
  21. void echo( int sd ) {
  22. int len,n;
  23. char *bufin;
  24. struct sockaddr_in remote;
  25.  
  26. /* need to know how big address struct is, len must be set before the
  27. call to recvfrom!!! */
  28.  
  29. len = sizeof(remote);
  30.  
  31. bufin = calloc(MAXBUF, sizeof(char));
  32.  
  33. while (1) {
  34. /* read a datagram from the socket (put result in bufin) */
  35. n=recvfrom(sd,bufin,MAXBUF,0,(struct sockaddr *)&remote,&len);
  36.  
  37. /* print out the address of the sender */
  38. printf("Got a datagram from %s port %d\n",
  39. inet_ntoa(remote.sin_addr), ntohs(remote.sin_port));
  40.  
  41. if (n<0) {
  42. perror("Error receiving data");
  43. } else {
  44. printf("GOT %d BYTES\n",n);
  45. /* Got something, just send it back */
  46. sendto(sd, bufin, n, 0, (struct sockaddr *)&remote, len);
  47. }
  48. }
  49. free(bufin);
  50. }
  51.  
  52. /* server main routine */
  53.  
  54. int main() {
  55. int ld;
  56. struct sockaddr_in skaddr;
  57. int length;
  58.  
  59. {
  60. #if defined(_WIN32)
  61. WSADATA wsaData;
  62. if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) {
  63. printf("Error at WSAStartup()\n");
  64. return 1;
  65. }
  66. #endif
  67. }
  68.  
  69. /* create a socket
  70. IP protocol family (PF_INET)
  71. UDP protocol (SOCK_DGRAM)
  72. */
  73.  
  74. if ((ld = socket( PF_INET, SOCK_DGRAM, 0 )) < 0) {
  75. printf("Problem creating socket\n");
  76. exit(1);
  77. }
  78.  
  79. /* establish our address
  80. address family is AF_INET
  81. our IP address is INADDR_ANY (any of our IP addresses)
  82. the port number is assigned by the kernel
  83. */
  84.  
  85. skaddr.sin_family = AF_INET;
  86. skaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  87. skaddr.sin_port = htons(0);
  88.  
  89. if (bind(ld, (struct sockaddr *) &skaddr, sizeof(skaddr))<0) {
  90. printf("Problem binding\n");
  91. exit(0);
  92. }
  93.  
  94. /* find out what port we were assigned and print it out */
  95.  
  96. length = sizeof( skaddr );
  97. if (getsockname(ld, (struct sockaddr *) &skaddr, &length)<0) {
  98. printf("Error getsockname\n");
  99. exit(1);
  100. }
  101.  
  102. /* port number's are network byte order, we have to convert to
  103. host byte order before printing !
  104. */
  105. printf("The server UDP port number is %d\n",ntohs(skaddr.sin_port));
  106.  
  107. /* Go echo every datagram we get */
  108. echo(ld);
  109. return(0);
  110. }
Add Comment
Please, Sign In to add comment