Advertisement
kyobreck

C++ First Winsock Client

Mar 24th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. // Actual client.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include <sdkddkver.h>
  7. #include <conio.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. #include <Ws2tcpip.h>
  12. #include <WinSock2.h>
  13. #include <Windows.h>
  14. #include <iostream>
  15. #include <signal.h>
  16.  
  17.  
  18. #include <sys\types.h>
  19.  
  20. #pragma comment(lib, "Ws2_32.lib")
  21. #pragma comment(lib, "Mswsock.lib")
  22. #pragma comment(lib, "AdvApi32.lib")
  23.  
  24.  
  25. #define WIN32_LEAN_AND_MEAN
  26.  
  27. #define DEFAULT_BUFLEN 512
  28. #define DEFAULT_PORT "27015"
  29. #define DEFAULT_LOOPBACK "127.0.0.1"
  30.  
  31.  
  32.  
  33. int _tmain(int argc, _TCHAR* argv[])
  34. {
  35.  
  36. WSADATA WSAdata1;
  37. SOCKET connectSocket = INVALID_SOCKET;
  38. struct addrinfo *result = NULL;
  39. struct addrinfo *ptr = NULL;
  40. struct addrinfo hints;
  41.  
  42. char *sendbuf = "This is a test";
  43. char rcvbuf[DEFAULT_BUFLEN];
  44. int iResult;
  45. int recvbuflen = DEFAULT_BUFLEN;
  46.  
  47. //validate the parameters
  48. if (argc != 2)
  49. {
  50. printf("Usage server-name %s\n", argv[0]);
  51. return 1;
  52. }
  53.  
  54. //initialize winsock
  55. iResult = WSAStartup(MAKEWORD(2, 2), &WSAdata1);
  56. if (iResult != 0)
  57. {
  58. std::cout << "Error initializing Winsock\n";
  59. }
  60.  
  61. ZeroMemory(&hints, sizeof(hints));
  62. hints.ai_family = AF_INET;
  63. hints.ai_socktype = SOCK_STREAM;
  64. hints.ai_protocol = IPPROTO_TCP;
  65.  
  66. //Resolve server address and port
  67. iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result); //so default address seems to work? test with tyler
  68. //but if anything its a problem with cmd's input mroe than anything
  69. //right now
  70. if (iResult != 0)
  71. {
  72.  
  73. std::cout << "Getaddrinfo failed with: " << WSAGetLastError() << '\n';
  74. WSACleanup();
  75. return 1;
  76. }
  77.  
  78. //Connect to first address we fine
  79. for (ptr = result; ptr != NULL; ptr = ptr->ai_next)
  80. {
  81. //create file descriptor
  82. connectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
  83. if (connectSocket == INVALID_SOCKET)
  84. {
  85. std::cout << "Error getting file descriptor with : " << WSAGetLastError() << '\n';
  86. WSACleanup();
  87. return 1;
  88. }
  89.  
  90. //connect to address
  91. iResult = connect(connectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
  92. if (iResult == SOCKET_ERROR)
  93. {
  94. closesocket(connectSocket);
  95. connectSocket = INVALID_SOCKET;
  96. continue;
  97. }
  98. break;
  99. }
  100.  
  101. freeaddrinfo(result);
  102.  
  103. if (connectSocket == INVALID_SOCKET)
  104. {
  105. std::cout << "Unable to connect to server...\n";
  106. WSACleanup();
  107. return 1;
  108. }
  109.  
  110. //send an initial buffer
  111. iResult = send(connectSocket, sendbuf, (int)strlen(sendbuf), 0);
  112. if (iResult == SOCKET_ERROR)
  113. {
  114. std::cout << "Error sending information with: " << WSAGetLastError() << '\n';
  115. closesocket(connectSocket);
  116. WSACleanup();
  117. return 1;
  118. }
  119.  
  120. std::cout << "Bytes sent: " << iResult;
  121.  
  122. //shutdown connection since no more data will be sent
  123. iResult = shutdown(connectSocket, SD_SEND);
  124. if (iResult == SOCKET_ERROR)
  125. {
  126. std::cout << "Error shutting down socket with: " << WSAGetLastError() << '\n';
  127. closesocket(connectSocket);
  128. WSACleanup();
  129. return 1;
  130. }
  131.  
  132. //recieve till peer closes connection
  133. do
  134. {
  135. iResult = recv(connectSocket, rcvbuf, recvbuflen, 0);
  136. if (iResult > 0)
  137. {
  138. std::cout << "Bytes recieved: " << iResult;
  139. }
  140. else if (iResult == 0)
  141. {
  142. std::cout << "Connection closed" << '\n';
  143. }
  144. else
  145. {
  146. std::cout << "Recievure failure with: " << WSAGetLastError();
  147. }
  148.  
  149. } while (iResult > 0);
  150.  
  151. //cleanup
  152. closesocket(connectSocket);
  153. WSACleanup();
  154.  
  155.  
  156. char a;
  157. std::cin >> a;
  158. return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement