Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <winsock2.h>
  3. #include <ws2tcpip.h>
  4. #include <iostream>
  5. #include <string>
  6. #include <thread>
  7.  
  8. using namespace std;
  9.  
  10. #pragma comment (lib, "Ws2_32.lib")
  11.  
  12. #define DEFAULT_BUFLEN 512
  13. #define IP_ADDRESS "192.168.0.4"
  14. #define DEFAULT_PORT "3504"
  15.  
  16. struct client_type
  17. {
  18. SOCKET socket;
  19. int id;
  20. char received_message[DEFAULT_BUFLEN];
  21. };
  22.  
  23. int process_client(client_type &new_client);
  24. int main();
  25.  
  26. int process_client(client_type &new_client)
  27. {
  28. while (1)
  29. {
  30. memset(new_client.received_message, 0, DEFAULT_BUFLEN);
  31.  
  32. if (new_client.socket != 0)
  33. {
  34. int iResult = recv(new_client.socket, new_client.received_message, DEFAULT_BUFLEN, 0);
  35.  
  36. if (iResult != SOCKET_ERROR)
  37. cout << new_client.received_message << endl;
  38. else
  39. {
  40. cout << "recv() failed: " << WSAGetLastError() << endl;
  41. break;
  42. }
  43. }
  44. }
  45.  
  46. if (WSAGetLastError() == WSAECONNRESET)
  47. cout << "The server has disconnected" << endl;
  48.  
  49. return 0;
  50. }
  51.  
  52. int main()
  53. {
  54. WSAData wsa_data;
  55. struct addrinfo *result = NULL, *ptr = NULL, hints;
  56. string sent_message = "";
  57. client_type client = { INVALID_SOCKET, -1, "" };
  58. int iResult = 0;
  59. string message;
  60.  
  61. cout << "Starting Client...\n";
  62.  
  63. // Initialize Winsock
  64. iResult = WSAStartup(MAKEWORD(2, 2), &wsa_data);
  65. if (iResult != 0) {
  66. cout << "WSAStartup() failed with error: " << iResult << endl;
  67. return 1;
  68. }
  69.  
  70. ZeroMemory(&hints, sizeof(hints));
  71. hints.ai_family = AF_UNSPEC;
  72. hints.ai_socktype = SOCK_STREAM;
  73. hints.ai_protocol = IPPROTO_TCP;
  74.  
  75. cout << "Connecting...\n";
  76.  
  77. // Resolve the server address and port
  78. iResult = getaddrinfo(static_cast<const char*>(IP_ADDRESS), DEFAULT_PORT, &hints, &result);
  79. if (iResult != 0) {
  80. cout << "getaddrinfo() failed with error: " << iResult << endl;
  81. WSACleanup();
  82. system("pause");
  83. return 1;
  84. }
  85.  
  86. // Attempt to connect to an address until one succeeds
  87. for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
  88.  
  89. // Create a SOCKET for connecting to server
  90. client.socket = socket(ptr->ai_family, ptr->ai_socktype,
  91. ptr->ai_protocol);
  92. if (client.socket == INVALID_SOCKET) {
  93. cout << "socket() failed with error: " << WSAGetLastError() << endl;
  94. WSACleanup();
  95. system("pause");
  96. return 1;
  97. }
  98.  
  99. // Connect to server.
  100. iResult = connect(client.socket, ptr->ai_addr, (int)ptr->ai_addrlen);
  101. if (iResult == SOCKET_ERROR) {
  102. closesocket(client.socket);
  103. client.socket = INVALID_SOCKET;
  104. continue;
  105. }
  106. break;
  107. }
  108.  
  109. freeaddrinfo(result);
  110.  
  111. if (client.socket == INVALID_SOCKET) {
  112. cout << "Unable to connect to server!" << endl;
  113. WSACleanup();
  114. system("pause");
  115. return 1;
  116. }
  117.  
  118. cout << "Successfully Connected" << endl;
  119.  
  120. //Obtain id from server for this client;
  121. recv(client.socket, client.received_message, DEFAULT_BUFLEN, 0);
  122. message = client.received_message;
  123.  
  124. if (message != "Server is full")
  125. {
  126. client.id = atoi(client.received_message);
  127.  
  128. thread my_thread(process_client, client);
  129.  
  130. while (1)
  131. {
  132. getline(cin, sent_message);
  133. iResult = send(client.socket, sent_message.c_str(), strlen(sent_message.c_str()), 0);
  134.  
  135. if (iResult <= 0)
  136. {
  137. cout << "send() failed: " << WSAGetLastError() << endl;
  138. break;
  139. }
  140. }
  141.  
  142. //Shutdown the connection since no more data will be sent
  143. my_thread.detach();
  144. }
  145. else
  146. cout << client.received_message << endl;
  147.  
  148. cout << "Shutting down socket..." << endl;
  149. iResult = shutdown(client.socket, SD_SEND);
  150. if (iResult == SOCKET_ERROR) {
  151. cout << "shutdown() failed with error: " << WSAGetLastError() << endl;
  152. closesocket(client.socket);
  153. WSACleanup();
  154. system("pause");
  155. return 1;
  156. }
  157.  
  158. closesocket(client.socket);
  159. WSACleanup();
  160. system("pause");
  161. return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement