peterzig

[PWŚS] Prosty Serwer / Klient

Feb 25th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 KB | None | 0 0
  1. protip: Win32 Console Application -> Empty Project
  2.  
  3. //////////////////Server/////////////////////////////
  4.  
  5. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  6.  
  7. #pragma comment(lib,"ws2_32.lib")
  8. #include <WinSock2.h>
  9. #include <iostream>
  10.  
  11. int main()
  12. {
  13.     //WinSock Startup
  14.     WSAData wsaData;
  15.     WORD DllVersion = MAKEWORD(2, 1);
  16.     if (WSAStartup(DllVersion, &wsaData) != 0) //If WSAStartup returns anything other than 0, then that means an error has occured in the WinSock Startup.
  17.     {
  18.         MessageBoxA(NULL, "WinSock startup failed", "Error", MB_OK | MB_ICONERROR);
  19.         return 0;
  20.     }
  21.  
  22.     SOCKADDR_IN addr; //Address that we will bind our listening socket to
  23.     int addrlen = sizeof(addr); //length of the address (required for accept call)
  24.     addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //Broadcast locally
  25.     addr.sin_port = htons(1111); //Port
  26.     addr.sin_family = AF_INET; //IPv4 Socket
  27.  
  28.     SOCKET sListen = socket(AF_INET, SOCK_STREAM, NULL); //Create socket to listen for new connections
  29.     bind(sListen, (SOCKADDR*)&addr, sizeof(addr)); //Bind the address to the socket
  30.     listen(sListen, SOMAXCONN); //Places sListen socket in a state in which it is listening for an incoming connection. Note:SOMAXCONN = Socket Oustanding Max Connections
  31.  
  32.     SOCKET newConnection; //Socket to hold the client's connection
  33.     newConnection = accept(sListen, (SOCKADDR*)&addr, &addrlen); //Accept a new connection
  34.     if (newConnection == 0) //If accepting the client connection failed
  35.     {
  36.         std::cout << "Failed to accept the client's connection." << std::endl;
  37.     }
  38.     else //If client connection properly accepted
  39.     {
  40.         std::cout << "Klient polaczony!" << std::endl;
  41.         char MOTD[256] = "Udalo sie przeslac wiadomosc z klienta na serwer!"; //Create buffer with message of the day
  42.         send(newConnection, MOTD, sizeof(MOTD), NULL); //Send MOTD buffer
  43.     }
  44.  
  45.     system("pause");
  46.     return 0;
  47. }
  48.  
  49. //////////////////END OF SERVER ///////////////
  50.  
  51. /////////////////////CLIENT////////////////////
  52.  
  53. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  54.  
  55. #pragma comment(lib,"ws2_32.lib")
  56. #include <WinSock2.h>
  57. #include <iostream>
  58.  
  59. int main()
  60. {
  61.     //Winsock Startup
  62.     WSAData wsaData;
  63.     WORD DllVersion = MAKEWORD(2, 1);
  64.     if (WSAStartup(DllVersion, &wsaData) != 0) //If WSAStartup returns anything other than 0, then that means an error has occured in the WinSock Startup.
  65.     {
  66.         MessageBoxA(NULL, "Winsock startup failed", "Error", MB_OK | MB_ICONERROR);
  67.         exit(1);
  68.     }
  69.  
  70.     SOCKADDR_IN addr; //Address to be binded to our Connection socket
  71.     int sizeofaddr = sizeof(addr); //Need sizeofaddr for the connect function
  72.     addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //Address = localhost (this pc)
  73.     addr.sin_port = htons(1111); //Port = 1111
  74.     addr.sin_family = AF_INET; //IPv4 Socket
  75.  
  76.     SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL); //Set Connection socket
  77.     if (connect(Connection, (SOCKADDR*)&addr, sizeofaddr) != 0) //If we are unable to connect...
  78.     {
  79.         MessageBoxA(NULL, "Failed to Connect", "Error", MB_OK | MB_ICONERROR);
  80.         return 0; //Failed to Connect
  81.     }
  82.     std::cout << "Polaczono z serwerem" << std::endl;
  83.  
  84.     char MOTD[256];
  85.     recv(Connection, MOTD, sizeof(MOTD), NULL); //Receive Message of the Day buffer into MOTD array
  86.     std::cout << "Wiadomosc:" << MOTD << std::endl;
  87.     while (true)
  88.     {
  89.         Sleep(10);
  90.     }
  91. }
  92.  
  93. //////////////END OF CLIENT////////////////////
Advertisement
Add Comment
Please, Sign In to add comment