peterzig

[PWŚS] Serwer UDP Nowy

Mar 21st, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. // [PWŚS] Server.cpp : Defines the entry point for the console application.
  2. //
  3. #include"stdafx.h"
  4. #include<stdio.h>
  5. #include<winsock2.h>
  6.  
  7. #pragma comment(lib,"ws2_32.lib") //Winsock Library
  8.  
  9. #define BUFLEN 512  //Max length of buffer
  10. #define PORT 27015   //The port on which to listen for incoming data
  11.  
  12. int main()
  13. {
  14.     SOCKET s;
  15.     struct sockaddr_in server, si_other;
  16.     int slen, recv_len;
  17.     char buf[BUFLEN];
  18.     WSADATA wsa;
  19.  
  20.     slen = sizeof(si_other);
  21.  
  22.     //Initialise winsock
  23.     printf("\nInitialising Winsock...");
  24.     if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
  25.     {
  26.         printf("Failed. Error Code : %d", WSAGetLastError());
  27.         exit(EXIT_FAILURE);
  28.     }
  29.     printf("Initialised.\n");
  30.  
  31.     //Create a socket
  32.     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
  33.     {
  34.         printf("Could not create socket : %d", WSAGetLastError());
  35.     }
  36.     printf("Socket created.\n");
  37.  
  38.     //Prepare the sockaddr_in structure
  39.     server.sin_family = AF_INET;
  40.     server.sin_addr.s_addr = INADDR_ANY;
  41.     server.sin_port = htons(PORT);
  42.  
  43.     //Bind
  44.     if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
  45.     {
  46.         printf("Bind failed with error code : %d", WSAGetLastError());
  47.         exit(EXIT_FAILURE);
  48.     }
  49.     puts("Bind done");
  50.  
  51.     //keep listening for data
  52.     while (1)
  53.     {
  54.         printf("Waiting for data...");
  55.         fflush(stdout);
  56.  
  57.         //clear the buffer by filling null, it might have previously received data
  58.         memset(buf, '\0', BUFLEN);
  59.  
  60.         //try to receive some data, this is a blocking call
  61.         if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == SOCKET_ERROR)
  62.         {
  63.             printf("recvfrom() failed with error code : %d", WSAGetLastError());
  64.             exit(EXIT_FAILURE);
  65.         }
  66.  
  67.         //print details of the client/peer and the data received
  68.         printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
  69.         printf("Data: %s\n", buf);
  70.  
  71.         //now reply the client with the same data
  72.         if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == SOCKET_ERROR)
  73.         {
  74.             printf("sendto() failed with error code : %d", WSAGetLastError());
  75.             exit(EXIT_FAILURE);
  76.         }
  77.     }
  78.  
  79.     closesocket(s);
  80.     WSACleanup();
  81.  
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment