Advertisement
peterzig

[PWŚS] UDP Server/Klient

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