Advertisement
seba101

(PWŚS) UDP - Serwer

Mar 11th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "stdafx.h"
  3. #include <locale.h>
  4. #include <WinSock2.h>
  5. #include <conio.h>
  6.  
  7. #pragma comment(lib, "Ws2_32.lib")
  8.  
  9. void CheckErrorByCode(int code)
  10. {
  11.  
  12.     char Buff[1024];
  13.     FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, NULL, Buff, sizeof(Buff), NULL);
  14.     printf(" %s", Buff);
  15.  
  16. }
  17.  
  18. int main(int argc, char** argv) {
  19.     setlocale(LC_ALL, "Polish");
  20.  
  21.  
  22.     WSADATA wsa;
  23.     SOCKADDR_IN addr;
  24.     addr.sin_family = AF_INET;
  25.     addr.sin_port = htons(19390);
  26.     addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  27.  
  28.  
  29.     char BuffToSend2[] = "Editable";
  30.     char BuffToShow2[2048];
  31.     char Host[512];
  32.  
  33.     int result = WSAStartup(MAKEWORD(2, 0), &wsa);
  34.  
  35.     if (result != 1)
  36.     {
  37.         printf("WSAStartup initialization: ");
  38.         CheckErrorByCode(result);
  39.     }
  40.     else
  41.     {
  42.         printf("WSAStartup initialization error! Error code: %d", result);
  43.         CheckErrorByCode(result);
  44.     }
  45.  
  46.     result = gethostname(Host, sizeof(Host));
  47.  
  48.     if (result == 1)
  49.     {
  50.         printf("Get hostname function error! Error code: %d", result);
  51.         CheckErrorByCode(result);
  52.     }
  53.  
  54.  
  55.  
  56.     SOCKET Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  57.     bind(Socket, (SOCKADDR*)&addr, sizeof(addr));
  58.  
  59.  
  60.     for (int i = 0; i < 2048; i++)
  61.     {
  62.         BuffToShow2[i] = NULL;
  63.     }
  64.  
  65.     int AddrSize = sizeof(addr);
  66.     recvfrom(Socket, BuffToShow2, sizeof(BuffToShow2), 0, (SOCKADDR*)&addr, &AddrSize);
  67.  
  68.     int len = strlen(BuffToShow2);
  69.  
  70.     strncat(BuffToSend2,BuffToShow2,strlen(BuffToShow2));
  71.  
  72.     sendto(Socket, BuffToSend2, strlen(BuffToSend2), 0, (SOCKADDR*)&addr, sizeof(addr));
  73.  
  74.     closesocket(Socket);
  75.     _getch();
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement