Advertisement
obernardovieira

UDP Socket (WinSock) Server&Client

Apr 8th, 2014
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.67 KB | None | 0 0
  1. //http://www.binarytides.com/udp-socket-programming-in-winsock/
  2.  
  3.  
  4. //server.cpp
  5.  
  6. /*
  7.     Simple UDP Server
  8.     Silver Moon ( m00n.silv3r@gmail.com )
  9. */
  10.  
  11. #include<stdio.h>
  12. #include<winsock2.h>
  13.  
  14. #pragma comment(lib,"ws2_32.lib") //Winsock Library
  15.  
  16. #define BUFLEN 512  //Max length of buffer
  17. #define PORT 8888   //The port on which to listen for incoming data
  18.  
  19. int main()
  20. {
  21.     SOCKET s;
  22.     struct sockaddr_in server, si_other;
  23.     int slen , recv_len;
  24.     char buf[BUFLEN];
  25.     WSADATA wsa;
  26.  
  27.     slen = sizeof(si_other) ;
  28.      
  29.     //Initialise winsock
  30.     printf("\nInitialising Winsock...");
  31.     if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
  32.     {
  33.         printf("Failed. Error Code : %d",WSAGetLastError());
  34.         exit(EXIT_FAILURE);
  35.     }
  36.     printf("Initialised.\n");
  37.      
  38.     //Create a socket
  39.     if((s = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
  40.     {
  41.         printf("Could not create socket : %d" , WSAGetLastError());
  42.         exit(EXIT_FAILURE);
  43.     }
  44.     printf("Socket created.\n");
  45.      
  46.     //Prepare the sockaddr_in structure
  47.     server.sin_family = AF_INET;
  48.     server.sin_addr.s_addr = INADDR_ANY;
  49.     server.sin_port = htons( PORT );
  50.      
  51.     //Bind
  52.     if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
  53.     {
  54.         printf("Bind failed with error code : %d" , WSAGetLastError());
  55.         exit(EXIT_FAILURE);
  56.     }
  57.     puts("Bind done");
  58.  
  59.     //keep listening for data
  60.     while(1)
  61.     {
  62.         printf("Waiting for data...");
  63.         fflush(stdout);
  64.          
  65.         //clear the buffer by filling null, it might have previously received data
  66.         memset(buf,'\0', BUFLEN);
  67.          
  68.         //try to receive some data, this is a blocking call
  69.         if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == SOCKET_ERROR)
  70.         {
  71.             printf("recvfrom() failed with error code : %d" , WSAGetLastError());
  72.             exit(EXIT_FAILURE);
  73.         }
  74.          
  75.         //print details of the client/peer and the data received
  76.         printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
  77.         printf("Data: %s\n" , buf);
  78.          
  79.         //now reply the client with the same data
  80.         if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == SOCKET_ERROR)
  81.         {
  82.             printf("sendto() failed with error code : %d" , WSAGetLastError());
  83.             exit(EXIT_FAILURE);
  84.         }
  85.     }
  86.  
  87.     closesocket(s);
  88.     WSACleanup();
  89.      
  90.     return 0;
  91. }
  92.  
  93. //client.cpp
  94.  
  95. /*
  96.     Simple udp client
  97.     Silver Moon (m00n.silv3r@gmail.com)
  98. */
  99. #include<stdio.h>
  100. #include<winsock2.h>
  101.  
  102. #pragma comment(lib,"ws2_32.lib") //Winsock Library
  103.  
  104. #define SERVER "127.0.0.1"  //ip address of udp server
  105. #define BUFLEN 512  //Max length of buffer
  106. #define PORT 8888   //The port on which to listen for incoming data
  107.  
  108. int main(void)
  109. {
  110.     struct sockaddr_in si_other;
  111.     int s, slen=sizeof(si_other);
  112.     char buf[BUFLEN];
  113.     char message[BUFLEN];
  114.     WSADATA wsa;
  115.  
  116.     //Initialise winsock
  117.     printf("\nInitialising Winsock...");
  118.     if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
  119.     {
  120.         printf("Failed. Error Code : %d",WSAGetLastError());
  121.         exit(EXIT_FAILURE);
  122.     }
  123.     printf("Initialised.\n");
  124.      
  125.     //create socket
  126.     if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
  127.     {
  128.         printf("socket() failed with error code : %d" , WSAGetLastError());
  129.         exit(EXIT_FAILURE);
  130.     }
  131.      
  132.     //setup address structure
  133.     memset((char *) &si_other, 0, sizeof(si_other));
  134.     si_other.sin_family = AF_INET;
  135.     si_other.sin_port = htons(PORT);
  136.     si_other.sin_addr.S_un.S_addr = inet_addr(SERVER);
  137.      
  138.     //start communication
  139.     while(1)
  140.     {
  141.         printf("Enter message : ");
  142.         gets(message);
  143.          
  144.         //send the message
  145.         if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
  146.         {
  147.             printf("sendto() failed with error code : %d" , WSAGetLastError());
  148.             exit(EXIT_FAILURE);
  149.         }
  150.          
  151.         //receive a reply and print it
  152.         //clear the buffer by filling null, it might have previously received data
  153.         memset(buf,'\0', BUFLEN);
  154.         //try to receive some data, this is a blocking call
  155.         if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == SOCKET_ERROR)
  156.         {
  157.             printf("recvfrom() failed with error code : %d" , WSAGetLastError());
  158.             exit(EXIT_FAILURE);
  159.         }
  160.          
  161.         puts(buf);
  162.     }
  163.  
  164.     closesocket(s);
  165.     WSACleanup();
  166.  
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement