Advertisement
Leeen

Client#1

Apr 27th, 2020
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include "pch.h"
  2. #pragma comment(lib, "ws2_32.lib")
  3. #include <winsock2.h>
  4. #include <iostream>
  5. #include <string>
  6.  
  7. #pragma warning(disable: 4996)
  8.  
  9. using namespace std;
  10.  
  11. int main(int argc, char* argv[]) {
  12.     WSAData wsaData;
  13.     WORD DLLVersion = MAKEWORD(2, 1);
  14.     if (WSAStartup(DLLVersion, &wsaData) != 0) {
  15.         std::cout << "Error" << std::endl;
  16.         exit(1);
  17.     }
  18.  
  19.     SOCKADDR_IN addr;
  20.     int sizeofaddr = sizeof(addr);
  21.     addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  22.     addr.sin_port = htons(1111);
  23.     addr.sin_family = AF_INET;
  24.     SOCKET Connection;
  25.     Connection = socket(AF_INET, SOCK_STREAM, NULL);
  26.     if (connect(Connection, (SOCKADDR*)&addr, sizeof(addr)) != 0) {
  27.         cout << "Error: failed connect to server.\n";
  28.         return 1;
  29.     }
  30.     cout << "Connected!\n";
  31.  
  32.     string msg1;
  33.     while (true) {
  34.         getline(cin, msg1);
  35.         int msg_size = msg1.size();
  36.         send(Connection, (char*)&msg_size, sizeof(int), NULL);
  37.         send(Connection, msg1.c_str(), msg_size, NULL);
  38.         Sleep(10);
  39.     }
  40.  
  41.     system("pause");
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement