Mihailo21

TCPKlientResenje1

Nov 20th, 2023
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  2. #define WIN32_LEAN_AND_MEAN
  3.  
  4. #include <windows.h>
  5. #include <winsock2.h>
  6. #include <ws2tcpip.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include "conio.h"
  10.  
  11. #pragma comment (lib, "Ws2_32.lib")
  12. #pragma comment (lib, "Mswsock.lib")
  13. #pragma comment (lib, "AdvApi32.lib")
  14.  
  15. #define SERVER_IP_ADDRESS "127.0.0.1"
  16. #define SERVER_PORT 27016
  17. #define BUFFER_SIZE 256
  18.  
  19. // TCP client that use blocking sockets
  20. int main()
  21. {
  22.     // Socket used to communicate with server
  23.     SOCKET connectSocket = INVALID_SOCKET;
  24.  
  25.     // Variable used to store function return value
  26.     int iResult;
  27.  
  28.     // Buffer we will use to store message
  29.     char dataBuffer[BUFFER_SIZE];
  30.  
  31.     // WSADATA data structure that is to receive details of the Windows Sockets implementation
  32.     WSADATA wsaData;
  33.  
  34.     // Initialize windows sockets library for this process
  35.     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
  36.     {
  37.         printf("WSAStartup failed with error: %d\n", WSAGetLastError());
  38.         return 1;
  39.     }
  40.  
  41.     // create a socket
  42.     connectSocket = socket(AF_INET,
  43.         SOCK_STREAM,
  44.         IPPROTO_TCP);
  45.  
  46.     if (connectSocket == INVALID_SOCKET)
  47.     {
  48.         printf("socket failed with error: %ld\n", WSAGetLastError());
  49.         WSACleanup();
  50.         return 1;
  51.     }
  52.  
  53.     // Create and initialize address structure
  54.     sockaddr_in serverAddress;
  55.     serverAddress.sin_family = AF_INET;                             // IPv4 protocol
  56.     serverAddress.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS);   // ip address of server
  57.     serverAddress.sin_port = htons(SERVER_PORT);                    // server port
  58.  
  59.     // Connect to server specified in serverAddress and socket connectSocket
  60.     if (connect(connectSocket, (SOCKADDR*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR)
  61.     {
  62.         printf("Unable to connect to server.\n");
  63.         closesocket(connectSocket);
  64.         WSACleanup();
  65.         return 1;
  66.     }
  67.  
  68.     bool end = false;
  69.  
  70.     do {
  71.  
  72.         // Receive  message from server
  73.         iResult = recv(connectSocket, dataBuffer, BUFFER_SIZE, 0);
  74.  
  75.         if (iResult > 0)    // Check if message is successfully received
  76.         {
  77.             dataBuffer[iResult] = '\0';
  78.  
  79.             // Log message text
  80.             printf("Server sent: %s.\n", dataBuffer);
  81.  
  82.             if (strstr(dataBuffer, "POBEDILI") || strstr(dataBuffer, "IZGUBILI"))
  83.                 end = true;
  84.  
  85.         }
  86.         else if (iResult == 0)  // Check if shutdown command is received
  87.         {
  88.             // Connection was closed successfully
  89.             printf("Connection with server closed.\n");
  90.             closesocket(connectSocket);
  91.             WSACleanup();
  92.             return 0;
  93.         }
  94.         else    // There was an error during recv
  95.         {
  96.  
  97.             printf("recv failed with error: %d\n", WSAGetLastError());
  98.             closesocket(connectSocket);
  99.             WSACleanup();
  100.             return 1;
  101.         }
  102.  
  103.         if (end)
  104.             break;
  105.  
  106.         // Read string from user into outgoing buffer
  107.         printf("Enter word on selected letter to send: ");
  108.         gets_s(dataBuffer, BUFFER_SIZE);
  109.  
  110.  
  111.         // Send message to server using connected socket
  112.         iResult = send(connectSocket, dataBuffer, (int)strlen(dataBuffer), 0);
  113.  
  114.         // Check result of send function
  115.         if (iResult == SOCKET_ERROR)
  116.         {
  117.             printf("send failed with error: %d\n", WSAGetLastError());
  118.             closesocket(connectSocket);
  119.             WSACleanup();
  120.             return 1;
  121.         }
  122.  
  123.  
  124.     } while (true);
  125.  
  126.  
  127.     // Shutdown the connection since we're done
  128.     iResult = shutdown(connectSocket, SD_BOTH);
  129.  
  130.     // Check if connection is succesfully shut down.
  131.     if (iResult == SOCKET_ERROR)
  132.     {
  133.         printf("Shutdown failed with error: %d\n", WSAGetLastError());
  134.         closesocket(connectSocket);
  135.         WSACleanup();
  136.         return 1;
  137.     }
  138.  
  139.     // For demonstration purpose
  140.     printf("\nPress any key to exit: ");
  141.     _getch();
  142.  
  143.  
  144.     // Close connected socket
  145.     closesocket(connectSocket);
  146.  
  147.     // Deinitialize WSA library
  148.     WSACleanup();
  149.  
  150.     return 0;
  151. }
Add Comment
Please, Sign In to add comment