Advertisement
Guest User

wsTCPClient.c

a guest
Jul 2nd, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #include <winsock2.h>
  2. #include <ws2tcpip.h>
  3. #include <stdio.h>
  4.  
  5.  
  6. #pragma comment(lib, "Ws2_32.lib")
  7.  
  8. int main()  {
  9.   WSDATA wsaData;
  10.   int iResult,
  11.       PORT,
  12.       HOST;
  13.  
  14.   char *msg = "Hello I'am Client!\n",
  15.         recvbuf[1024];
  16.  
  17.   struct addrinfo *result = NULL,
  18.                   *ptr = NULL,
  19.                   Cltsock;
  20.  
  21.  
  22.  
  23.     /*Passing argument */
  24.   if(argc == 3) {
  25.     PORT = argv[2];
  26.     HOST = argv[1];
  27.     msg = argv[3];
  28.   }
  29.   else  {
  30.     printf("Usage: %s [HOST] [PORT] [Msg]", argv[0]);
  31.   }
  32.  
  33.   /* Initialize Winsock*/
  34.   if((iResult = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)  {
  35.     printf("WSAStartup failed: %d\n", iResult);
  36.     return 1;
  37.   }
  38.  
  39.   /* Passing parameter to struct */
  40.   ZeroMemory(&Cltsock, sizeof(Cltsock));
  41.   Cltsock.ai_family = AF_UNSPEC;
  42.   Cltsock.ai_socktype = SOCK_STREAM;
  43.   Cltsock.ai_protocol = IPPROTO_TCP;
  44.  
  45. /* Get address info*/
  46.   if((iResult = getaddrinfo(argv[1], PORT, &Cltsock, &result)) != 0)  {
  47.     printf("getaddressinfo failed: %d\n", iResult);
  48.     WSACleanup();
  49.     return 1;
  50.   }
  51.  
  52.   /*Create Socket*/
  53.   SOCKET ConnectSocket = INVALID_SOCKET;
  54.  
  55.   if((ConnectSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol)) == INVALID_SOCKET)  {
  56.     printf("Error at socket(): %ld\n", WSAGetLastError());
  57.     freeaddrinfo();
  58.     WSACleanup();
  59.     return 1;
  60.   }
  61.  
  62.   /* Connect to server */
  63.   if((iResult = connect(ConnectSocket, result->ai_addr, (int)result->ai_addrlen)) == SOCKET_ERROR)  {
  64.     closesocket(ConnectSocket);
  65.     ConnectSocket = INVALID_SOCKET;
  66.   }
  67.  
  68.   /* Send an initial buffer */
  69.   if((iResult = send(ConnectSocket, msg, (int)strlen(msg), 0)) == SOCKET_ERROR) {
  70.     printf("send failed: %d\n", WSAGetLastError());
  71.     closesocket(ConnectSocket);
  72.     WSACleanup();
  73.     return 1;
  74.   }
  75.  
  76.   printf("Bytes sent: %ld\n", iResult);
  77.  
  78.   /* Shutdown the connection for sending since no more data will be sent */
  79.   if(iResult == SOCKET_ERROR) {
  80.     printf("Shutdown failed: %d\n", WSAGetLastError());
  81.     closesocket(ConnectSocket);
  82.     WSACleanup();
  83.     return 1;
  84.   }
  85.  
  86. /* Recv data until the server close the connecion */
  87.   do {
  88.     iResult = recv(ConnectSocket, recvbuf, 1024, 0);
  89.     if(iResult > 0) {
  90.       printf("Bytes received: %d\n", iResult);
  91.     }
  92.     else if(iResult == 0) {
  93.       printf("Connection closed\n");
  94.     }
  95.     else {
  96.       printf("Recv failed: %d\n", WSAGetLastError());
  97.     }
  98.   } while(iResult > 0);
  99.  
  100. /*Disconnection*/
  101.   iResult = shutdown(Connection, SD_SEND);
  102.   if(iResult == SOCKET_ERROR) {
  103.     printf("shutdown failed: %d\n", WSAGetLastError());
  104.     closesocket(ConnectSocket);
  105.     WSACleanup();
  106.     return 1;
  107.   }
  108.  
  109.   closesocket(ConnectSocket);
  110.   WSACleanup();
  111.   return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement