Mihailo21

KlientUDPResen

Nov 20th, 2023
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. // UDP client that uses blocking sockets
  2.  
  3. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  4. #define WIN32_LEAN_AND_MEAN
  5.  
  6. #include <windows.h>
  7. #include <winsock2.h>
  8. #include <ws2tcpip.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include "conio.h"
  12.  
  13. #pragma comment (lib, "Ws2_32.lib")
  14. #pragma comment (lib, "Mswsock.lib")
  15. #pragma comment (lib, "AdvApi32.lib")
  16.  
  17. #define SERVER_IP_ADDRESS "127.0.0.1"
  18. #define SERVER_PORT 15001
  19. #define BUFFER_SIZE 512
  20.  
  21.  
  22. int main()
  23. {
  24.     // Server address
  25.     sockaddr_in serverAddress;
  26.  
  27.     // Size of sockaddr_in structure
  28.     int sockAddrSize = sizeof(serverAddress);
  29.  
  30.     // Buffer we will use to store message
  31.     char dataBuffer[BUFFER_SIZE];
  32.  
  33.     // Port on server that will be used for communication with client
  34.     unsigned short serverPort = SERVER_PORT;
  35.  
  36.     // WSADATA data structure that is to receive details of the Windows Sockets implementation
  37.     WSADATA wsaData;
  38.  
  39.     // Initialize windows sockets for this process
  40.     int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  41.     if (iResult != 0)
  42.     {
  43.         printf("WSAStartup failed with error: %d\n", iResult);
  44.         return 1;
  45.     }
  46.  
  47.     // Initialize serverAddress structure
  48.     memset((char*)&serverAddress, 0, sockAddrSize);
  49.     serverAddress.sin_family = AF_INET;
  50.     serverAddress.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS);
  51.     serverAddress.sin_port = htons(serverPort);
  52.  
  53.     // Create a socket
  54.     SOCKET clientSocket = socket(AF_INET,      // IPv4 address famly
  55.         SOCK_DGRAM,   // datagram socket
  56.         IPPROTO_UDP); // UDP
  57.  
  58. // Check if socket creation succeeded
  59.     if (clientSocket == INVALID_SOCKET)
  60.     {
  61.         printf("Creating socket failed with error: %d\n", WSAGetLastError());
  62.         WSACleanup();
  63.         return 1;
  64.     }
  65.  
  66.     char previous_message[BUFFER_SIZE];
  67.  
  68.     while (true)
  69.     {
  70.         // Read string from user into outgoing buffer
  71.         printf("Enter message to send: ");
  72.         gets_s(dataBuffer, BUFFER_SIZE);
  73.  
  74.         // Check if client closing command is entered
  75.         if (!strcmp(dataBuffer, "stop client"))
  76.         {
  77.             break;
  78.         }
  79.  
  80.         // Send message to server
  81.         iResult = sendto(clientSocket,
  82.             dataBuffer,
  83.             strlen(dataBuffer),
  84.             0,
  85.             (SOCKADDR*)&serverAddress,
  86.             sockAddrSize);
  87.  
  88.         // Check result of send function
  89.         if (iResult == SOCKET_ERROR)
  90.         {
  91.             printf("sendto failed with error: %d\n", WSAGetLastError());
  92.             closesocket(clientSocket);
  93.             WSACleanup();
  94.             return 1;
  95.         }
  96.  
  97.  
  98.         // Check if 'server close command' is issued. Client is also closing, after server is closed.
  99.         //compare current and previous message 
  100.         if (!strcmp(previous_message, dataBuffer))
  101.         {
  102.             printf("Two identical messages were sent consecutively. Server is being closed.\n");
  103.             break;
  104.         }
  105.         //store current message for next comparison
  106.         strcpy(previous_message, dataBuffer);
  107.  
  108.  
  109.  
  110.         // Receive server message
  111.         iResult = recvfrom(clientSocket,
  112.             dataBuffer,
  113.             BUFFER_SIZE,
  114.             0,
  115.             (SOCKADDR*)&serverAddress,
  116.             &sockAddrSize);
  117.  
  118.         if (iResult == SOCKET_ERROR)
  119.         {
  120.             printf("recvfrom failed with error: %d\n", WSAGetLastError());
  121.             continue;
  122.         }
  123.  
  124.         // Set end of string
  125.         dataBuffer[iResult] = '\0';
  126.         // Log message from server
  127.         printf("Message from server: %s.\n\n", dataBuffer);
  128.  
  129.     }
  130.  
  131.     // Close application
  132.     iResult = closesocket(clientSocket);
  133.     if (iResult == SOCKET_ERROR)
  134.     {
  135.         printf("closesocket failed with error: %d\n", WSAGetLastError());
  136.         WSACleanup();
  137.         return 1;
  138.     }
  139.  
  140.     WSACleanup();
  141.  
  142.     return 0;
  143. }
  144.  
Add Comment
Please, Sign In to add comment