SHARE
TWEET

TCP_SERVER_02

skindervik Jun 30th, 2020 (edited) 886 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.     sockaddr_in client_address; //NEW
  27.     int client_address_size = sizeof(client_address);  //NEW
  28.  
  29.     listen_socket = accept(listen_socket, (sockaddr*)&client_address, &client_address_size);
  30.  
  31.     //RECEIVE
  32.     char client_ip[256];  //NEW
  33.  
  34.     inet_ntop(AF_INET, &client_address.sin_addr, client_ip, 256);  //NEW
  35.  
  36.     char buffer[1024]{ 0 };
  37.  
  38.     recv(listen_socket, buffer, sizeof(buffer), 0);
  39.  
  40.     std::cout << client_ip << ": " << buffer << std::endl;
  41.  
  42.     WSAGetLastError();
  43.     //PAUSE
  44.     system("pause");
  45.     closesocket(listen_socket);
  46.     WSACleanup();
  47. }
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