Mihailo21

ServerUDPResen1

Nov 20th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.65 KB | None | 0 0
  1. // UDP server
  2. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  3. #define WIN32_LEAN_AND_MEAN
  4.  
  5. #include <windows.h>
  6. #include <winsock2.h>
  7. #include <ws2tcpip.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include "conio.h"
  11.  
  12. #pragma comment (lib, "Ws2_32.lib")
  13. #pragma comment (lib, "Mswsock.lib")
  14. #pragma comment (lib, "AdvApi32.lib")
  15.  
  16. #define SERVER_PORT 15001
  17. #define BUFFER_SIZE 512    
  18.  
  19.  
  20. int main()
  21. {
  22.     // Server address
  23.     sockaddr_in serverAddress;
  24.  
  25.     // Size of sockaddr_in structure
  26.     int sockAddrSize = sizeof(serverAddress);
  27.  
  28.     // Buffer we will use to receive client message
  29.     char dataBuffer[BUFFER_SIZE];
  30.  
  31.     // Variable used to store function return value
  32.     int iResult;
  33.  
  34.     // WSADATA data structure that is to receive details of the Windows Sockets implementation
  35.     WSADATA wsaData;
  36.  
  37.     // Initialize windows sockets library for this process
  38.     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
  39.     {
  40.         printf("WSAStartup failed with error: %d\n", WSAGetLastError());
  41.         return 1;
  42.     }
  43.  
  44.     // Initialize serverAddress structure used by bind function
  45.     memset((char*)&serverAddress, 0, sockAddrSize);
  46.     serverAddress.sin_family = AF_INET;         // Set server address protocol family
  47.     serverAddress.sin_addr.s_addr = INADDR_ANY; // Use all available addresses of server
  48.     serverAddress.sin_port = htons(SERVER_PORT);
  49.  
  50.     // Create a socket
  51.     SOCKET serverSocket = socket(AF_INET,      // IPv4 address famly
  52.         SOCK_DGRAM,   // Datagram socket type
  53.         IPPROTO_UDP); // UDP
  54.  
  55. // Check if socket creation succeeded
  56.     if (serverSocket == INVALID_SOCKET)
  57.     {
  58.         printf("Creating socket failed with error: %d\n", WSAGetLastError());
  59.         WSACleanup();
  60.         return 1;
  61.     }
  62.  
  63.     // Bind port number and local address to socket
  64.     iResult = bind(serverSocket, (SOCKADDR*)&serverAddress, sockAddrSize);
  65.  
  66.     if (iResult == SOCKET_ERROR)
  67.     {
  68.         printf("Socket bind failed with error: %d\n", WSAGetLastError());
  69.         closesocket(serverSocket);
  70.         WSACleanup();
  71.         return 1;
  72.     }
  73.  
  74.     printf("Simple UDP server started and waiting client messages.\n");
  75.  
  76.     int big_letters, small_letters, other_chars;
  77.     char previous[BUFFER_SIZE];
  78.  
  79.  
  80.     // Main server loop
  81.     while (1)
  82.     {
  83.         // ClientAddress will be set from recvfrom
  84.         sockaddr_in clientAddress;
  85.         memset(&clientAddress, 0, sockAddrSize);
  86.  
  87.         // Set whole buffer to zero
  88.         memset(dataBuffer, 0, BUFFER_SIZE);
  89.  
  90.         // Receive client message
  91.         iResult = recvfrom(serverSocket,
  92.             dataBuffer,
  93.             BUFFER_SIZE,
  94.             0,
  95.             (SOCKADDR*)&clientAddress,
  96.             &sockAddrSize);
  97.  
  98.         if (iResult == SOCKET_ERROR)
  99.         {
  100.             printf("recvfrom failed with error: %d\n", WSAGetLastError());
  101.             continue;
  102.         }
  103.  
  104.         // Set end of string
  105.         dataBuffer[iResult] = '\0';
  106.  
  107.         char ipAddress[16]; // 15 spaces for decimal notation (for example: "192.168.100.200") + '\0'
  108.  
  109.         // Copy client ip to local char[]
  110.         strcpy_s(ipAddress, sizeof(ipAddress), inet_ntoa(clientAddress.sin_addr));
  111.  
  112.         // Convert port number from network byte order to host byte order
  113.         unsigned short clientPort = ntohs(clientAddress.sin_port);
  114.  
  115.  
  116.         // Check big, small letters and other characters in message
  117.         big_letters = small_letters = other_chars = 0;
  118.         for (int i = 0; i < strlen(dataBuffer); i++)
  119.         {
  120.  
  121.             // 65 = A, 90 = Z
  122.             if (65 <= dataBuffer[i] && dataBuffer[i] <= 90)
  123.             {
  124.                 ++big_letters;
  125.             }
  126.             // 97 = a, 122 = z
  127.             else if (97 <= dataBuffer[i] && dataBuffer[i] <= 122)
  128.             {
  129.                 ++small_letters;
  130.             }
  131.             else
  132.                 ++other_chars;
  133.         }
  134.  
  135.         // Log client ip address, port and message text
  136.         printf("Client connected from ip: %s, port: %d, sent: %s.\n", ipAddress, clientPort, dataBuffer);
  137.  
  138.         //Log number of big, small letters and other characters in message
  139.         printf("Message has %d characters. Big letters: %d, small letters: %d and other characters: %d.\n", iResult, big_letters, small_letters, other_chars);
  140.  
  141.  
  142.         // Check if client sent two identical messages consecutively
  143.         //compare current and previous message
  144.         if (!strcmp(previous, dataBuffer))
  145.         {
  146.             printf("Client sent two identical messages consecutively. Server is closing.\n");
  147.             break;
  148.         }
  149.         //store current message for next comparison
  150.         strcpy(previous, dataBuffer);
  151.  
  152.  
  153.  
  154.         // Send message to client
  155.         iResult = sendto(serverSocket,
  156.             dataBuffer,
  157.             strlen(dataBuffer),
  158.             0,
  159.             (SOCKADDR*)&clientAddress,
  160.             sockAddrSize);
  161.  
  162.         if (iResult == SOCKET_ERROR)
  163.         {
  164.             printf("sendto failed with error: %d\n", WSAGetLastError());
  165.             continue;
  166.         }
  167.     }
  168.  
  169.     // Close server application
  170.     iResult = closesocket(serverSocket);
  171.     if (iResult == SOCKET_ERROR)
  172.     {
  173.         printf("closesocket failed with error: %ld\n", WSAGetLastError());
  174.         WSACleanup();
  175.         return 1;
  176.     }
  177.  
  178.     printf("Server successfully shut down.\n");
  179.  
  180.     WSACleanup();
  181.     return 0;
  182. }
Add Comment
Please, Sign In to add comment