Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. // CLIENT UDP
  2. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  3. #pragma comment(lib, "ws2_32.lib")
  4. #ifndef UNICODE
  5. #define UNICODE
  6. #endif
  7. #define WIN32_LEAN_AND_MEAN
  8. #include <WinSock2.h>
  9. #include <iostream>
  10. #include <WS2tcpip.h>
  11. #include <stdio.h>
  12.  
  13. using namespace std;
  14.  
  15. int main() {
  16.     WSAData wsaData;
  17.     WORD DLLVersion = MAKEWORD(2, 2);
  18.     if (WSAStartup(DLLVersion, &wsaData) != 0)
  19.     {
  20.         MessageBoxA(NULL, "Start Winsocka zostal przerwany.", "Error", MB_OK | MB_ICONERROR);
  21.         exit(1);
  22.     }
  23.  
  24.     SOCKET SendSocket = INVALID_SOCKET;
  25.     SOCKADDR_IN RecvAddr;
  26.  
  27.     SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  28.     if (SendSocket == INVALID_SOCKET) {
  29.         cout << "Blad polaczenia..." << endl;
  30.         exit(1);
  31.     }
  32.  
  33.     RecvAddr.sin_family = AF_INET;
  34.     RecvAddr.sin_port = htons(27015);
  35.     RecvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  36.     cout << "Przesylam..." << endl;
  37.     int addrlen = sizeof(RecvAddr);
  38.     int L;
  39.     unsigned NetL;
  40.     char Mess[10];
  41.  
  42.  
  43.     cout << "Podaj liczbe: " << endl;
  44.     cin >> L;
  45.     std::cout << std::endl;
  46.     NetL = htonl((unsigned)L);
  47.     sendto(SendSocket, (char*)&NetL, sizeof(int), 0, (SOCKADDR *)& RecvAddr, sizeof(RecvAddr)); //wysylanie
  48.    
  49.     while (recvfrom(SendSocket, (char*)&Mess, sizeof(Mess), 0, (SOCKADDR *)& RecvAddr, &addrlen) > 0) {
  50.         cout << Mess << endl;
  51.         Sleep(2000);
  52.     }
  53.     system("pause");
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement