SHARE
TWEET

Untitled

a guest Dec 11th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <WS2tcpip.h>
  3. #include <string>
  4. #include <conio.h>
  5. #include <limits>
  6. #include <thread>
  7. #include <windows.h>
  8. #pragma comment(lib, "ws2_32.lib")
  9. using namespace std;
  10.  
  11. void getmessage(char buf[4096], SOCKET sock, char host[NI_MAXHOST])
  12. {
  13.     //Wait
  14.     int bytesReceived;
  15.     while (true) {
  16.     ZeroMemory(buf, 4096);
  17.     bytesReceived = recv(sock, buf, 4096, 0);
  18.     if (bytesReceived == 0)
  19.     {
  20.         std::cout << "\t\t\tRecieved 0 bytes" << std::endl;
  21.         Sleep(1000);
  22.         break;
  23.     }
  24.     else
  25.     {
  26.         std::cout << "\t\t\t" << host << ": " << buf << "\r\a";
  27.  
  28.  
  29.     }
  30.     }
  31.    
  32.    
  33.  
  34. }
  35.  
  36. int main()
  37. {
  38.     string ipAddress;
  39.     int port;
  40.     cout << "Ip address to connect to: ";
  41.     cin >> ipAddress;
  42.     cout << "Port: ";
  43.     cin >> port;
  44.     //Initialize Winsock
  45.     WSAData data;
  46.     WORD ver = MAKEWORD(2, 2);
  47.     int wsResult = WSAStartup(ver, &data);
  48.     if (wsResult != 0)
  49.     {
  50.         cerr << "Failed to start" << endl;
  51.         return 0;
  52.     }
  53.  
  54.     //Create socket
  55.     SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
  56.     if (sock == INVALID_SOCKET)
  57.     {
  58.         cerr << "Failed to start" << endl;
  59.         return 0;
  60.     }
  61.  
  62.     //Fill in a hint structure
  63.     sockaddr_in hint;
  64.     hint.sin_family = AF_INET;
  65.     hint.sin_port = htons(port);
  66.     inet_pton(AF_INET, ipAddress.c_str(), &hint.sin_addr);
  67.     //Connect to server
  68.  
  69.     int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint));
  70.     if (connResult == SOCKET_ERROR)
  71.     {
  72.         int LastError = WSAGetLastError();
  73.         if (LastError == 10013) {
  74.             cout << "Permission denied" << endl;
  75.         }
  76.         if (LastError == 10048) {
  77.             cout << "Address already in use." << endl;
  78.         }
  79.         if (LastError == 10049) {
  80.             cout << "Cannot assign requested address." << endl;
  81.         }
  82.         if (LastError == 10047) {
  83.             cout << "Address family not supported by protocol family." << endl;
  84.         }
  85.         if (LastError == 10037) {
  86.             cout << "Operation already in process." << endl;
  87.         }
  88.         if (LastError == 10053) {
  89.             cout << "Software caused connection abort." << endl;
  90.         }
  91.         while (LastError == 10060)
  92.         {
  93.  
  94.             if (connect(sock, (sockaddr*)&hint, sizeof(hint)))
  95.             {
  96.                 goto messaging;
  97.             }
  98.  
  99.             else {
  100.                 cout << "Connection timed out." << endl;
  101.             }
  102.         }
  103.         if (LastError == 10054) {
  104.  
  105.             cout << "Connection reset by peer." << endl;
  106.         }
  107.         if (LastError == 10039) {
  108.             cout << "Destion address required." << endl;
  109.         }
  110.         if (LastError == 10014) {
  111.             cout << "Invalid address." << endl;
  112.         }
  113.         if (LastError == 10064) {
  114.             cout << "Host is down." << endl;
  115.         }
  116.         if (LastError == 10065) {
  117.             cout << "No route to host." << endl;
  118.         }
  119.         if (LastError == 10036) {
  120.             cout << "Operation now in process." << endl;
  121.         }
  122.         if (LastError == 10050) {
  123.             cout << "Network is down." << endl;
  124.         }
  125.         if (LastError == 10061) {
  126.             cout << "Connection refused" << endl;
  127.         }
  128.         else {
  129.             cerr << "Error: #" << WSAGetLastError() << endl;
  130.         }
  131.         cout << "Press 'ENTER' to close program";
  132.  
  133.         cin.ignore();
  134.         cin.get();
  135.         closesocket(sock);
  136.         WSACleanup();
  137.         return 0;
  138.     }
  139.  
  140.     //Do-while loop to send and recieve data
  141. messaging:
  142.     char buf[4096];
  143.     string userInput;
  144.     do
  145.     {
  146.         //Promt
  147.         system("cls");
  148.         char host[NI_MAXHOST]; //Remote name
  149.         char service[NI_MAXSERV]; //Service or port
  150.  
  151.         ZeroMemory(host, NI_MAXHOST);
  152.         ZeroMemory(service, NI_MAXSERV);
  153.         if (getnameinfo((sockaddr*)&hint, sizeof(hint), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
  154.         {
  155.             system("cls");
  156.             std::cout << host << " joined the chat" << std::endl;
  157.  
  158.         }
  159.         else
  160.         {
  161.             inet_ntop(AF_INET, &hint.sin_addr, host, NI_MAXHOST);
  162.             std::cout << host << " joined the chat" << std::endl;
  163.         }
  164.        
  165.         //Wait
  166.         thread getmessaget(getmessage, buf, sock, host);
  167. cout << "Your message: ";
  168.         cin.ignore();
  169.         getline(cin, userInput);
  170.  
  171.         //Send
  172.         int sendResult = send(sock, userInput.c_str(), userInput.size() + 1, 0);
  173.         if (sendResult == SOCKET_ERROR)
  174.         {
  175.  
  176.             cout << "error";
  177.             Sleep(2000);
  178.             goto close;
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.         }
  186.  
  187.     } while (userInput.size() > 0);
  188.     //Gracefully close down everything
  189. close:
  190.     closesocket(sock);
  191.     WSACleanup();
  192. }
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