nukede

Untitled

Jan 6th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.15 KB | None | 0 0
  1. // ConsoleApplication2.cpp : Defines the entry point for the console application.
  2. //
  3. #define WIN32_LEAN_AND_MEAN
  4. #include <iostream>
  5. #include <windows.h>
  6. #include <winsock2.h>
  7. #include <ws2tcpip.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10.  
  11.  
  12. // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
  13. #pragma comment (lib, "Ws2_32.lib")
  14. #pragma comment (lib, "Mswsock.lib")
  15. #pragma comment (lib, "AdvApi32.lib")
  16.  
  17.  
  18. #define DEFAULT_BUFLEN 512
  19. #define DEFAULT_PORT "27015"
  20.  
  21.  
  22. int __cdecl main(int argc, char **argv)
  23. {
  24.    
  25.     WSADATA wsaData;
  26.     int iResult;
  27.     int iSendResult;
  28.     int i = 0;
  29.  
  30.     SOCKET ListenSocket = INVALID_SOCKET;
  31.     SOCKET ClientSocket = INVALID_SOCKET;
  32.     SOCKET ConnectSocket = INVALID_SOCKET;
  33.  
  34.     char *sendbuf = "Online\0";
  35.     struct addrinfo *result = NULL;
  36.     struct addrinfo hints;
  37.     struct addrinfo *ptr = NULL;
  38.  
  39.     char recvbuf[DEFAULT_BUFLEN];
  40.     char buffer[DEFAULT_BUFLEN];
  41.     int recvbuflen = DEFAULT_BUFLEN;
  42.  
  43.  
  44.    
  45.     // Initialize Winsock
  46.     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  47.     if (iResult != 0) {
  48.         printf("WSAStartup failed with error: %d\n", iResult);
  49.         return 1;
  50.     }
  51.  
  52.     //if there is no ip, then wait for connection.
  53.     if (argc != 2) { //DEBUG
  54.         printf("Okay, wait for connection...");
  55.         ZeroMemory(&hints, sizeof(hints));
  56.         hints.ai_family = AF_INET;
  57.         hints.ai_socktype = SOCK_STREAM;
  58.         hints.ai_protocol = IPPROTO_TCP;
  59.         hints.ai_flags = AI_PASSIVE;
  60.  
  61.         // Resolve the server address and port
  62.         iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
  63.         if (iResult != 0) {
  64.             printf("getaddrinfo failed with error: %d\n", iResult);
  65.             WSACleanup();
  66.             return 1;
  67.         }
  68.  
  69.         // Create a SOCKET for connecting to server
  70.         ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  71.         if (ListenSocket == INVALID_SOCKET) {
  72.             printf("socket failed with error: %ld\n", WSAGetLastError());
  73.             freeaddrinfo(result);
  74.             WSACleanup();
  75.             return 1;
  76.         }
  77.         // Setup the TCP listening socket
  78.         iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
  79.         if (iResult == SOCKET_ERROR) {
  80.             printf("bind failed with error: %d\n", WSAGetLastError());
  81.             freeaddrinfo(result);
  82.             closesocket(ListenSocket);
  83.             WSACleanup();
  84.             return 1;
  85.         }
  86.  
  87.         freeaddrinfo(result);
  88.  
  89.         iResult = listen(ListenSocket, SOMAXCONN);
  90.         if (iResult == SOCKET_ERROR) {
  91.             printf("listen failed with error: %d\n", WSAGetLastError());
  92.             closesocket(ListenSocket);
  93.             WSACleanup();
  94.             return 1;
  95.         }
  96.  
  97.         // Accept a client socket
  98.         ClientSocket = accept(ListenSocket, NULL, NULL);
  99.         if (ClientSocket == INVALID_SOCKET) {
  100.             printf("accept failed with error: %d\n", WSAGetLastError());
  101.             closesocket(ListenSocket);
  102.             WSACleanup();
  103.             return 1;
  104.         }
  105.  
  106.         // No longer need server socket
  107.         closesocket(ListenSocket);
  108.  
  109.         // Receive until the peer shuts down the connection
  110.         do {
  111.  
  112.             iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  113.             if (iResult > 0) {
  114.                 std::cout << "\nMessage: ";
  115.                 for (i = 0; i < iResult; ++i)
  116.                     printf("%c", recvbuf[i]);
  117.                
  118.                 std::cout << "\nEnter Your Message: ";
  119.                 std::cin >> buffer;
  120.  
  121.  
  122.                 iSendResult = send(ClientSocket, buffer, (int)strlen(buffer), 0);
  123.                 if (iSendResult == SOCKET_ERROR) {
  124.                     printf("send failed with error: %d\n", WSAGetLastError());
  125.                     closesocket(ClientSocket);
  126.                     WSACleanup();
  127.                     return 1;
  128.                 }
  129.  
  130.             }
  131.             else if (iResult == 0)
  132.                 printf("Connection closing...\n");
  133.             else  {
  134.                 printf("recv failed with error: %d\n", WSAGetLastError());
  135.                 closesocket(ClientSocket);
  136.                 WSACleanup();
  137.                 return 1;
  138.             }
  139.  
  140.         } while (iResult > 0);
  141.     }
  142.  
  143.  
  144.     else
  145.  
  146.  
  147.     {
  148.         std::cout << "Connect to " << argv[1];
  149.  
  150.         ZeroMemory(&hints, sizeof(hints));
  151.         hints.ai_family = AF_UNSPEC;
  152.         hints.ai_socktype = SOCK_STREAM;
  153.         hints.ai_protocol = IPPROTO_TCP;
  154.    
  155.         // Resolve the server address and port
  156.         iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);//DEBUG
  157.         if (iResult != 0) {
  158.             printf("getaddrinfo failed with error: %d\n", iResult);
  159.             WSACleanup();
  160.             return 1;
  161.         }
  162.  
  163.         // Attempt to connect to an address until one succeeds
  164.         for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
  165.  
  166.             // Create a SOCKET for connecting to server
  167.             ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
  168.                 ptr->ai_protocol);
  169.             if (ConnectSocket == INVALID_SOCKET) {
  170.                 printf("socket failed with error: %ld\n", WSAGetLastError());
  171.                 WSACleanup();
  172.                 return 1;
  173.             }
  174.  
  175.             // Connect to server.
  176.             iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
  177.             if (iResult == SOCKET_ERROR) {
  178.                 closesocket(ConnectSocket);
  179.                 ConnectSocket = INVALID_SOCKET;
  180.                 continue;
  181.             }
  182.             break;
  183.         }
  184.  
  185.         freeaddrinfo(result);
  186.  
  187.         if (ConnectSocket == INVALID_SOCKET) {
  188.             printf("Unable to connect to server!\n");
  189.             WSACleanup();
  190.             return 1;
  191.         }
  192.         // Send an initial buffer
  193.         iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
  194.         if (iResult == SOCKET_ERROR) {
  195.             printf("send failed with error: %d\n", WSAGetLastError());
  196.             closesocket(ConnectSocket);
  197.             WSACleanup();
  198.             return 1;
  199.         }
  200.  
  201.  
  202.         // Receive until the peer closes the connection
  203.         do {
  204.  
  205.             iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
  206.             if (iResult > 0)
  207.             {
  208.                 std::cout << "\nMessage: ";
  209.                 for (i = 0; i < iResult; ++i)
  210.                     printf("%c", recvbuf[i]);
  211.  
  212.                 std::cout << "\nEnter Your Message: ";
  213.                 std::cin >> buffer;
  214.  
  215.  
  216.                 iResult = send(ConnectSocket, buffer, (int)strlen(buffer), 0);
  217.                 if (iResult == SOCKET_ERROR) {
  218.                     printf("send failed with error: %d\n", WSAGetLastError());
  219.                     closesocket(ConnectSocket);
  220.                     WSACleanup();
  221.                     return 1;
  222.                 }
  223.             }
  224.             else if (iResult == 0)
  225.                 printf("Connection closed\n");
  226.             else
  227.                 printf("recv failed with error: %d\n", WSAGetLastError());
  228.  
  229.         } while (iResult > 0);
  230.  
  231.  
  232.         // shutdown the connection since no more data will be sent
  233.         iResult = shutdown(ConnectSocket, SD_SEND);
  234.         if (iResult == SOCKET_ERROR) {
  235.             printf("shutdown failed with error: %d\n", WSAGetLastError());
  236.             closesocket(ConnectSocket);
  237.             WSACleanup();
  238.             return 1;
  239.         }
  240.  
  241.         // cleanup
  242.         closesocket(ConnectSocket);
  243.         WSACleanup();
  244.        
  245.     }
  246.  
  247.    
  248.  
  249.    
  250.     return 0;
  251.  
  252.  
  253. }
Advertisement
Add Comment
Please, Sign In to add comment