Mihailo21

PeteUDPClient

Nov 21st, 2023
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 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"       // IPv4 address of server
  16. #define BUFFER_SIZE 512                     // Size of buffer that will be used for sending and receiving messages to client
  17.  
  18.  
  19. int main()
  20. {
  21.     // Server address structure
  22.     sockaddr_in serverAddress;
  23.  
  24.     // Size of server address structure
  25.     int sockAddrLen = sizeof(serverAddress);
  26.  
  27.     // Buffer that will be used for sending and receiving messages to client
  28.     char dataBuffer[BUFFER_SIZE];
  29.  
  30.     // WSADATA data structure that is used to receive details of the Windows Sockets implementation
  31.     WSADATA wsaData;
  32.    
  33.     // Initialize windows sockets for this process
  34.     int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  35.    
  36.     // Check if library is succesfully initialized
  37.     if (iResult != 0)
  38.     {
  39.         printf("WSAStartup failed with error: %d\n", iResult);
  40.         return 1;
  41.     }
  42.  
  43.    // Initialize memory for address structure
  44.     memset((char*)&serverAddress, 0, sizeof(serverAddress));       
  45.    
  46.     // Read server's port number
  47.     printf("Enter server port number (15011 or 15012):\n");
  48.     gets_s(dataBuffer, BUFFER_SIZE);
  49.     unsigned long serverPort = atoi(dataBuffer);
  50.  
  51.      // Initialize address structure of server
  52.     serverAddress.sin_family = AF_INET;                             // IPv4 address famly
  53.     serverAddress.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS);   // Set server IP address using string
  54.     serverAddress.sin_port = htons(serverPort);                 // Set server port
  55.  
  56.     // Create a socket
  57.     SOCKET clientSocket = socket(AF_INET,      // IPv4 address famly
  58.                                  SOCK_DGRAM,   // Datagram socket
  59.                                  IPPROTO_UDP); // UDP protocol
  60.  
  61.     // Check if socket creation succeeded
  62.     if (clientSocket == INVALID_SOCKET)
  63.     {
  64.         printf("Creating socket failed with error: %d\n", WSAGetLastError());
  65.         WSACleanup();
  66.         return 1;
  67.     }
  68.  
  69.    while (true)
  70.    {
  71.    
  72.         printf("Enter message to send:\n");
  73.  
  74.         // Read string from user into outgoing buffer
  75.         gets_s(dataBuffer, BUFFER_SIZE);
  76.  
  77.         //close client if end  message is entered
  78.         if ( !strcmp(dataBuffer,"end"))
  79.         {
  80.          printf("Client has finished. It's closing.\n");
  81.              break;
  82.         }
  83.    
  84.         // Send message to server
  85.      iResult = sendto(clientSocket,                     // Own socket
  86.                      dataBuffer,                        // Text of message
  87.                      strlen(dataBuffer),                // Message size
  88.                      0,                                 // No flags
  89.                      (SOCKADDR *)&serverAddress,        // Address structure of server (type, IP address and port)
  90.                      sizeof(serverAddress));            // Size of sockadr_in structure
  91.  
  92.         // Check if message is succesfully sent. If not, close client application
  93.         if (iResult == SOCKET_ERROR)
  94.         {
  95.         printf("sendto failed with error: %d\n", WSAGetLastError());
  96.         closesocket(clientSocket);
  97.         WSACleanup();
  98.         return 1;
  99.         }
  100.  
  101.    }
  102.  
  103.     // Only for demonstration purpose
  104.     printf("Press any key to exit: ");
  105.     _getch();
  106.  
  107.     // Close client application
  108.     iResult = closesocket(clientSocket);
  109.     if (iResult == SOCKET_ERROR)
  110.     {
  111.         printf("closesocket failed with error: %d\n", WSAGetLastError());
  112.         WSACleanup();
  113.         return 1;
  114.     }
  115.  
  116.     // Close Winsock library
  117.     WSACleanup();
  118.  
  119.     // Client has succesfully sent a message
  120.     return 0;
  121. }
  122.  
Add Comment
Please, Sign In to add comment