Advertisement
Alisator

Win client TCP/IP 2

Sep 2nd, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // WinSockTest.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4.  
  5. #define _CRT_SECURE_NO_DEPRECATE
  6. #define WIN32_LEAN_AND_MEAN
  7. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  8.  
  9. #include "stdafx.h"
  10. #include <windows.h>
  11. #include <winsock2.h>
  12. #include <ws2tcpip.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <time.h>
  16.  
  17.  
  18. //Winsock libs
  19. // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
  20. #pragma comment (lib, "Ws2_32.lib")
  21. #pragma comment (lib, "Mswsock.lib")
  22. #pragma comment (lib, "AdvApi32.lib")
  23.  
  24. #define DEFAULT_BUFLEN 512
  25. #define DEFAULT_PORT "27015"
  26. //test sending getrequest to google server
  27. //int callGoogle(FILE* fn, SOCKET ConnectSocket)
  28. //{
  29. //  //Send some data to google
  30. //  char *message, server_reply[2000];
  31. //  int recv_size;
  32. //  message = "GET / HTTP/1.1\r\n\r\n";
  33. //  if (send(ConnectSocket, message, strlen(message), 0) < 0)
  34. //  {
  35. //      fprintf(fn, "Send failed");
  36. //      return 1;
  37. //  }
  38. //  puts("Data Send\n");
  39. //
  40. //  //Receive a reply from the server
  41. //  if ((recv_size = recv(ConnectSocket, server_reply, 2000, 0)) == SOCKET_ERROR)
  42. //  {
  43. //      fprintf(fn, "Recv failed");
  44. //      return 1;
  45. //  }
  46. //
  47. //  fprintf(fn, "Reply received\n");
  48. //
  49. //  //Add a NULL terminating character to make it a proper string before printing
  50. //  server_reply[recv_size] = '\0';
  51. //  fprintf(fn, server_reply);
  52. //  return 0;
  53. //}
  54.  
  55. int main(int argc, char *argv[])
  56. {
  57.     WSADATA wsa;
  58.     SOCKET ConnectSocket;
  59.     FILE* fn;
  60.     int iResult;
  61.     struct addrinfo *result = NULL,
  62.         *ptr = NULL;
  63.  
  64.     time_t rawtime;
  65.     struct tm * timeinfo;
  66.  
  67.     time(&rawtime);
  68.     timeinfo = localtime(&rawtime);
  69.  
  70.     fn = fopen("D:\\log.txt", "a+");
  71.     fprintf(fn, "-------------------------\n%s", asctime(timeinfo));
  72.     fprintf(fn, "Initialising Winsock...\n");
  73.  
  74.     //-----------------------------
  75.     // Initialiaze a WinSock
  76.     if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
  77.     {
  78.         fprintf(fn, "Initialization socker failed with error: %d\n", WSAGetLastError());
  79.         return 1;
  80.     }
  81.     fprintf(fn, "Winsock initialised.\n");
  82.  
  83.     ConnectSocket = INVALID_SOCKET;
  84.  
  85.     //----------------------
  86.     // Create a SOCKET for connecting to server
  87.     ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  88.     if (ConnectSocket == INVALID_SOCKET) {
  89.         fprintf(fn, "Socket function failed with error: %ld\n", WSAGetLastError());
  90.         WSACleanup();
  91.         return 1;
  92.     }
  93.     fprintf(fn, "Socket created.\n");
  94.  
  95.     // IPv4 AF_INET sockets:
  96.     // Address Family : AF_INET(this is IP version 4)
  97.     // Type : SOCK_STREAM(this means connection oriented TCP protocol)
  98.     // Protocol : 0[or IPPROTO_TCP, IPPROTO_UDP]
  99.     // The sockaddr_in structure specifies the address family,
  100.     // IP address, and port of the server to be connected to.
  101.     sockaddr_in clientService;
  102.     clientService.sin_family = AF_INET;
  103.     //google address
  104.     //clientService.sin_addr.s_addr = inet_addr("216.58.214.206");
  105.     clientService.sin_addr.s_addr = inet_addr("192.168.0.14");
  106.     clientService.sin_port = htons(8888);
  107.  
  108.     //----------------------
  109.     // Connect to server.
  110.     iResult = connect(ConnectSocket, (SOCKADDR *)& clientService, sizeof(clientService));
  111.     if (iResult == SOCKET_ERROR) {
  112.         fprintf(fn, "Connection to remote server failed with error %d\n", WSAGetLastError());
  113.         closesocket(ConnectSocket);
  114.         ConnectSocket = INVALID_SOCKET;
  115.         return 1;
  116.     }
  117.     fprintf(fn, "Connected to remote server\n");
  118.  
  119.  
  120.    //----------------------
  121.     //message to be sent
  122.     char sendbuf[DEFAULT_BUFLEN];
  123.     int sendlen = sprintf(sendbuf, "%d", 0) + 1;
  124.     //int sendlen = 2;
  125.     //sendbuf[0] = 0;
  126.     //sendbuf[1] = 0;
  127.     char recvbuf[DEFAULT_BUFLEN];
  128.     int recvbuflen = DEFAULT_BUFLEN;
  129.  
  130.     // Send an initial buffer
  131.     iResult = send(ConnectSocket, sendbuf, sendlen, 0);
  132.     if (iResult == SOCKET_ERROR) {
  133.         fprintf(fn, "Send failed with error: %d\n", WSAGetLastError());
  134.         closesocket(ConnectSocket);
  135.         WSACleanup();
  136.         return 1;
  137.     }
  138.     fprintf(fn, "Bytes Sent: %ld\n", iResult);
  139.  
  140.     // shutdown the connection since no more data will be sent
  141.     //  Shutdown send operations.
  142.     iResult = shutdown(ConnectSocket, SD_SEND);
  143.     if (iResult == SOCKET_ERROR) {
  144.         fprintf(fn, "Shutdown failed with error: %d\n", WSAGetLastError());
  145.         closesocket(ConnectSocket);
  146.         WSACleanup();
  147.         return 1;
  148.     }
  149.     fprintf(fn, "Shutdown send operations.\n");
  150.  
  151.     // Receive until the peer closes the connection
  152.     do {
  153.         iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
  154.         if (iResult > 0)
  155.             fprintf(fn, "Bytes received: %d\n", iResult);
  156.         else if (iResult == 0)
  157.             fprintf(fn, "Connection closed\n");
  158.         else
  159.             fprintf(fn, "Recv failed with error: %d\n", WSAGetLastError());
  160.  
  161.     } while (iResult > 0);
  162.    
  163.  
  164.     // shutdown the connection since no more data will be sent
  165.     //  Shutdown recieve operations.
  166.     iResult = shutdown(ConnectSocket, SD_RECEIVE);
  167.     if (iResult == SOCKET_ERROR) {
  168.         fprintf(fn, "Shutdown failed with error: %d\n", WSAGetLastError());
  169.         closesocket(ConnectSocket);
  170.         WSACleanup();
  171.         return 1;
  172.     }
  173.     fprintf(fn, "Shutdown recieve operations.\n");
  174.  
  175.     fprintf(fn, recvbuf);
  176.  
  177.     // cleanup
  178.     closesocket(ConnectSocket);
  179.     WSACleanup();
  180.     fprintf(fn, "\nSocket closed\n");
  181.  
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement