Advertisement
Mihailo21

Postavka_Klient_UDP

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