SHARE
TWEET

TCP_SERVER_01

skindervik Jun 30th, 2020 (edited) 831 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<WS2tcpip.h>
  3. #include<thread>
  4. #include<sstream>
  5. #include<vector>
  6. #pragma comment (lib, "Ws2_32.lib")
  7.  
  8. void main() {
  9.     //INITIALIZE SOMETHING
  10.     WSADATA some_kind_of_data;
  11.     WSAStartup(MAKEWORD(2, 2), &some_kind_of_data);
  12.  
  13.     //CREATE LISTENING SOCKET
  14.     sockaddr_in listen_address;
  15.     listen_address.sin_family = AF_INET;
  16.     listen_address.sin_port = htons(666);
  17.     listen_address.sin_addr.S_un.S_addr = INADDR_ANY;
  18.  
  19.     SOCKET listen_socket = socket(AF_INET, SOCK_STREAM, 0);
  20.  
  21.     bind(listen_socket, (sockaddr*)&listen_address, sizeof(listen_address));
  22.  
  23.     listen(listen_socket, SOMAXCONN);
  24.  
  25.     //WAIT FOR CONNECTION
  26.  
  27.     listen_socket = accept(listen_socket, NULL, NULL);
  28.  
  29.     //RECEIVE MESSAGE
  30.  
  31.     char buffer[1024]{ 0 };
  32.  
  33.     recv(listen_socket, buffer, sizeof(buffer), 0);
  34.  
  35.     std::cout << buffer << std::endl;
  36.  
  37.     //PAUSE
  38.     system("pause");
  39.     closesocket(listen_socket);
  40.     WSACleanup();
  41. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top