Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. #define WIN32_LEAN_AND_MEAN
  6.  
  7. #include <windows.h>
  8. #include <winsock2.h>
  9. #include <ws2tcpip.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12.  
  13.  
  14. // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
  15. #pragma comment (lib, "Ws2_32.lib")
  16. #pragma comment (lib, "Mswsock.lib")
  17. #pragma comment (lib, "AdvApi32.lib")
  18.  
  19.  
  20. #define DEFAULT_BUFLEN 512
  21. #define DEFAULT_PORT "27015"
  22.  
  23. int __cdecl main(int argc, char **argv)
  24. {
  25. WSADATA wsaData;
  26. SOCKET ConnectSocket = INVALID_SOCKET;
  27. struct addrinfo *result = NULL,
  28. *ptr = NULL,
  29. hints;
  30. char *sendbuf = "this is a test";
  31. char recvbuf[DEFAULT_BUFLEN];
  32. int iResult;
  33. int recvbuflen = DEFAULT_BUFLEN;
  34.  
  35. // Validate the parameters
  36. if (argc != 2) {
  37. printf("usage: %s server-name\n", argv[0]);
  38. cin.get();
  39. return 1;
  40. }
  41.  
  42. // Initialize Winsock
  43. iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  44. if (iResult != 0) {
  45. printf("WSAStartup failed with error: %d\n", iResult);
  46. return 1;
  47. }
  48.  
  49. ZeroMemory( &hints, sizeof(hints) );
  50. hints.ai_family = AF_UNSPEC;
  51. hints.ai_socktype = SOCK_STREAM;
  52. hints.ai_protocol = IPPROTO_TCP;
  53.  
  54. // Resolve the server address and port
  55. iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
  56. if ( iResult != 0 ) {
  57. printf("getaddrinfo failed with error: %d\n", iResult);
  58. WSACleanup();
  59. return 1;
  60. }
  61.  
  62. // Attempt to connect to an address until one succeeds
  63. for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
  64.  
  65. // Create a SOCKET for connecting to server
  66. ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
  67. ptr->ai_protocol);
  68. if (ConnectSocket == INVALID_SOCKET) {
  69. printf("socket failed with error: %ld\n", WSAGetLastError());
  70. WSACleanup();
  71. return 1;
  72. }
  73.  
  74. // Connect to server.
  75. iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
  76. if (iResult == SOCKET_ERROR) {
  77. closesocket(ConnectSocket);
  78. ConnectSocket = INVALID_SOCKET;
  79. continue;
  80. }
  81. break;
  82. }
  83.  
  84. freeaddrinfo(result);
  85.  
  86. if (ConnectSocket == INVALID_SOCKET) {
  87. printf("Unable to connect to server!\n");
  88. WSACleanup();
  89. return 1;
  90. }
  91.  
  92. // Send an initial buffer
  93. iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
  94. if (iResult == SOCKET_ERROR) {
  95. printf("send failed with error: %d\n", WSAGetLastError());
  96. closesocket(ConnectSocket);
  97. WSACleanup();
  98. return 1;
  99. }
  100.  
  101. printf("Bytes Sent: %ld\n", iResult);
  102.  
  103. // shutdown the connection since no more data will be sent
  104. iResult = shutdown(ConnectSocket, SD_SEND);
  105. if (iResult == SOCKET_ERROR) {
  106. printf("shutdown failed with error: %d\n", WSAGetLastError());
  107. closesocket(ConnectSocket);
  108. WSACleanup();
  109. return 1;
  110. }
  111.  
  112. // Receive until the peer closes the connection
  113. do {
  114.  
  115. iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
  116. if ( iResult > 0 )
  117. printf("Bytes received: %d\n", iResult);
  118. else if ( iResult == 0 )
  119. printf("Connection closed\n");
  120. else
  121. printf("recv failed with error: %d\n", WSAGetLastError());
  122.  
  123. } while( iResult > 0 );
  124.  
  125. // cleanup
  126. closesocket(ConnectSocket);
  127. WSACleanup();
  128.  
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement