Advertisement
Guest User

Client

a guest
Jan 17th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. #define WIN32_LEAN_AND_MEAN
  2.  
  3. #include <windows.h>
  4. #include <winsock2.h>
  5. #include <ws2tcpip.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include "conio.h"
  9.  
  10. #pragma comment (lib, "Ws2_32.lib")
  11. #pragma comment (lib, "Mswsock.lib")
  12. #pragma comment (lib, "AdvApi32.lib")
  13.  
  14. #define SERVER_IP_ADDRESS "127.0.0.1"
  15. #define SERVER_PORT 27016
  16. #define BUFFER_SIZE 256
  17.  
  18. // TCP client that use blocking sockets
  19. int main()
  20. {
  21. // Socket used to communicate with server
  22. SOCKET connectSocket = INVALID_SOCKET;
  23.  
  24. // Variable used to store function return value
  25. int iResult;
  26.  
  27. // Buffer we will use to store message
  28. char dataBuffer[BUFFER_SIZE];
  29.  
  30. // WSADATA data structure that is to receive details of the Windows Sockets implementation
  31. WSADATA wsaData;
  32.  
  33. // Initialize windows sockets library for this process
  34. if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
  35. {
  36. printf("WSAStartup failed with error: %d\n", WSAGetLastError());
  37. return 1;
  38. }
  39.  
  40. // create a socket
  41. connectSocket = socket(AF_INET,
  42. SOCK_STREAM,
  43. IPPROTO_TCP);
  44.  
  45. if (connectSocket == INVALID_SOCKET)
  46. {
  47. printf("socket failed with error: %ld\n", WSAGetLastError());
  48. WSACleanup();
  49. return 1;
  50. }
  51.  
  52. // Create and initialize address structure
  53. sockaddr_in serverAddress;
  54. serverAddress.sin_family = AF_INET; // IPv4 protocol
  55. serverAddress.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS); // ip address of server
  56. serverAddress.sin_port = htons(SERVER_PORT); // server port
  57.  
  58. // Connect to server specified in serverAddress and socket connectSocket
  59. if (connect(connectSocket, (SOCKADDR*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR)
  60. {
  61. printf("Unable to connect to server.\n");
  62. closesocket(connectSocket);
  63. WSACleanup();
  64. return 1;
  65. }
  66.  
  67. // Read string from user into outgoing buffer
  68. printf("Enter message to send: ");
  69. gets_s(dataBuffer, BUFFER_SIZE);
  70.  
  71. // Send message to server using connected socket
  72. iResult = send( connectSocket, dataBuffer, (int)strlen(dataBuffer), 0 );
  73.  
  74. // Check result of send function
  75. if (iResult == SOCKET_ERROR)
  76. {
  77. printf("send failed with error: %d\n", WSAGetLastError());
  78. closesocket(connectSocket);
  79. WSACleanup();
  80. return 1;
  81. }
  82.  
  83. printf("Message successfully sent. Total bytes: %ld\n", iResult);
  84.  
  85. // Shutdown the connection since we're done
  86. iResult = shutdown(connectSocket, SD_BOTH);
  87.  
  88. // Check if connection is succesfully shut down.
  89. if (iResult == SOCKET_ERROR)
  90. {
  91. printf("Shutdown failed with error: %d\n", WSAGetLastError());
  92. closesocket(connectSocket);
  93. WSACleanup();
  94. return 1;
  95. }
  96.  
  97. // For demonstration purpose
  98. printf("\nPress any key to exit: ");
  99. getch();
  100.  
  101.  
  102. // Close connected socket
  103. closesocket(connectSocket);
  104.  
  105. // Deinitialize WSA library
  106. WSACleanup();
  107.  
  108. return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement