Advertisement
Leeen

Client#2

Apr 28th, 2020
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 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.         cout << "Error" << endl;
  16.         exit(1);
  17.     }
  18.  
  19.     DWORD cmode;
  20.     cmode &= ~ENABLE_LINE_INPUT;
  21.     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), cmode);
  22.  
  23.     SOCKADDR_IN addr;
  24.     int sizeofaddr = sizeof(addr);
  25.     addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  26.     addr.sin_port = htons(1111);
  27.     addr.sin_family = AF_INET;
  28.     SOCKET newConnection;
  29.     newConnection = socket(AF_INET, SOCK_STREAM, NULL);
  30.     if (connect(newConnection, (SOCKADDR*)&addr, sizeof(addr)) != 0) {
  31.         cout << "Error: failed connect to server.\n";
  32.         closesocket(newConnection);
  33.         WSACleanup();
  34.         return -1;
  35.     }
  36.     else
  37.         cout << "Connected!\n";
  38.  
  39.     char msg;
  40.     while (true) {
  41.         msg = getchar();
  42.         send(newConnection, &msg, sizeof(char), NULL);
  43.         if (msg == '\b')
  44.             cout << '\b' << ' ' << '\b';
  45.         else if (msg == '\n')
  46.             cout << endl;
  47.         else
  48.             cout << msg;
  49.     }
  50.  
  51.     WSACleanup();
  52.     closesocket(newConnection);
  53.  
  54.     system("pause");
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement