Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Nov 14th, 2018 86 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. // TCP-Client.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #define WIN32_LEAN_AND_MEAN
  5.  
  6. #include <windows.h>
  7. #include <winsock2.h>
  8. #include <ws2tcpip.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <iostream>
  12.  
  13. // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
  14. #pragma comment (lib, "Ws2_32.lib")
  15. #pragma comment (lib, "Mswsock.lib")
  16. #pragma comment (lib, "AdvApi32.lib")
  17.  
  18.  
  19. #define DEFAULT_BUFLEN 512
  20. #define DEFAULT_PORT "7777"
  21.  
  22. int __cdecl main(int argc, char **argv)
  23. {
  24.     printf("TCP-Client\n");
  25.     WSADATA wsaData;
  26.     SOCKET ConnectSocket = INVALID_SOCKET;
  27.     struct addrinfo *result = NULL,
  28.         *ptr = NULL,
  29.         hints;
  30.     const char *sendbuf = "this is a test";
  31.     char recvbuf[DEFAULT_BUFLEN];
  32.     int iResult;
  33.     int recvbuflen = DEFAULT_BUFLEN;
  34.  
  35.     // Initialize Winsock
  36.     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  37.     if (iResult != 0) {
  38.         printf("WSAStartup failed with error: %d\n", iResult);
  39.         getchar(); return 1;
  40.     }
  41.  
  42.     ZeroMemory(&hints, sizeof(hints));
  43.     hints.ai_family = AF_UNSPEC;
  44.     hints.ai_socktype = SOCK_STREAM;
  45.     hints.ai_protocol = IPPROTO_TCP;
  46.  
  47.     // Resolve the server address and port
  48.     iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
  49.     if (iResult != 0) {
  50.         printf("getaddrinfo failed with error: %d\n", iResult);
  51.         WSACleanup();
  52.         getchar(); return 1;
  53.     }
  54.  
  55.     // Attempt to connect to an address until one succeeds
  56.     for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
  57.  
  58.         // Create a SOCKET for connecting to server
  59.         ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
  60.             ptr->ai_protocol);
  61.         if (ConnectSocket == INVALID_SOCKET) {
  62.             printf("socket failed with error: %ld\n", WSAGetLastError());
  63.             WSACleanup();
  64.             getchar(); return 1;
  65.         }
  66.  
  67.         // Connect to server.
  68.         iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
  69.         if (iResult == SOCKET_ERROR) {
  70.             closesocket(ConnectSocket);
  71.             ConnectSocket = INVALID_SOCKET;
  72.             continue;
  73.         }
  74.         break;
  75.     }
  76.  
  77.     freeaddrinfo(result);
  78.  
  79.     if (ConnectSocket == INVALID_SOCKET) {
  80.         printf("Unable to connect to server!\n");
  81.         WSACleanup();
  82.         getchar(); return 1;
  83.     }
  84.     int randomSocket;
  85.     recv(ConnectSocket, (char *)&randomSocket, sizeof(int), 0);
  86.     randomSocket = (int)randomSocket;
  87.     std::cout << "odebrano: " << randomSocket << "\n";
  88.     closesocket(ConnectSocket);
  89.  
  90.  
  91.  
  92.  
  93.  
  94.    
  95.     // Resolve the server address and port
  96.     iResult = getaddrinfo(argv[1], (char*)randomSocket, &hints, &result);
  97.     if (iResult != 0) {
  98.         printf("getaddrinfo failed with error: %d\n", iResult);
  99.         WSACleanup();
  100.         getchar(); return 1;
  101.     }
  102.  
  103.     // Attempt to connect to an address until one succeeds
  104.     for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
  105.  
  106.         // Create a SOCKET for connecting to server
  107.         ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
  108.             ptr->ai_protocol);
  109.         if (ConnectSocket == INVALID_SOCKET) {
  110.             printf("socket failed with error: %ld\n", WSAGetLastError());
  111.             WSACleanup();
  112.             getchar(); return 1;
  113.         }
  114.  
  115.         // Connect to server.
  116.         iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
  117.         if (iResult == SOCKET_ERROR) {
  118.             closesocket(ConnectSocket);
  119.             ConnectSocket = INVALID_SOCKET;
  120.             continue;
  121.         }
  122.         break;
  123.     }
  124.  
  125.     freeaddrinfo(result);
  126.  
  127.     if (ConnectSocket == INVALID_SOCKET) {
  128.         printf("Unable to connect to server!\n");
  129.         WSACleanup();
  130.         getchar(); return 1;
  131.     }
  132.    
  133.     printf("Bytes Sent: %ld\n", iResult);
  134.  
  135.     recv(ConnectSocket, (char *)&randomSocket, sizeof(int), 0);
  136.     randomSocket = (int)randomSocket;
  137.     std::cout << "odebrano: " << randomSocket << "\n";
  138.  
  139.     // cleanup
  140.     closesocket(ConnectSocket);
  141.     WSACleanup();
  142.     getchar();
  143.     return 0;
  144. }
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