SHARE
TWEET

TCP_SERVER_03

skindervik Jun 30th, 2020 (edited) 1,019 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.     int status = 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.     status = bind(listen_socket, (sockaddr*)&listen_address, sizeof(listen_address));
  22.     if (status == SOCKET_ERROR) {
  23.         std::cout << "ABORT\n";
  24.         closesocket(listen_socket); //clean up
  25.         WSACleanup(); //clean up
  26.     }
  27.  
  28.     status = listen(listen_socket, SOMAXCONN);
  29.     if (status == SOCKET_ERROR) {
  30.         std::cout << "ABORT\n";
  31.         closesocket(listen_socket); //clean up
  32.         WSACleanup(); //clean up
  33.     }
  34.  
  35.     //WAIT FOR CONNECTION
  36.  
  37.     listen_socket = accept(listen_socket, NULL, NULL);
  38.     if (listen_socket != INVALID_SOCKET) {
  39.         std::cout << "ABORT\n";
  40.         closesocket(listen_socket); //clean up
  41.         WSACleanup(); //clean up
  42.     }
  43.  
  44.     //RECEIVE MESSAGE
  45.  
  46.     char buffer[1024]{ 0 };
  47.  
  48.     status = recv(listen_socket, buffer, sizeof(buffer), 0);
  49.     if (status < 0) {
  50.         std::cout << "ABORT\n";
  51.         closesocket(listen_socket); //clean up
  52.         WSACleanup(); //clean up
  53.     }
  54.  
  55.     std::cout << buffer << std::endl;
  56.  
  57.     //std::cout << WSAGetLastError() << std::endl;
  58.  
  59.     //PAUSE
  60.     system("pause");
  61.     closesocket(listen_socket);
  62.     WSACleanup();
  63. }
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