Advertisement
alien1337

Untitled

Jan 21st, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.03 KB | None | 0 0
  1. #define SERVER_IP_ADDRESS "127.0.0.1"       // IPv4 address of server
  2. #define SERVER_PORT 15000                   // Port number of server that will be used for communication with clients
  3. #define BUFFER_SIZE 512                     // Size of buffer that will be used for sending and receiving messages to client
  4. #define CLIENT_PORT 15011
  5.  
  6. int main()
  7. {
  8.     // Server address structure
  9.     sockaddr_in serverAddress;
  10.     sockaddr_in clientAddress;
  11.  
  12.     // Size of server address structure
  13.     int sockAddrLen = sizeof(serverAddress);
  14.  
  15.     // Buffer that will be used for sending and receiving messages to client
  16.     char dataBuffer[BUFFER_SIZE];
  17.  
  18.     // WSADATA data structure that is used to receive details of the Windows Sockets implementation
  19.     WSADATA wsaData;
  20.    
  21.     // Initialize windows sockets for this process
  22.     int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  23.    
  24.     // Check if library is succesfully initialized
  25.     if (iResult != 0)
  26.     {
  27.         printf("WSAStartup failed with error: %d\n", iResult);
  28.         return 1;
  29.     }
  30.  
  31.    // Initialize memory for address structure
  32.     memset((char*)&serverAddress, 0, sizeof(serverAddress));       
  33.    
  34.      // Initialize address structure of server
  35.     serverAddress.sin_family = AF_INET;                             // IPv4 address famly
  36.     serverAddress.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS);   // Set server IP address using string
  37.     serverAddress.sin_port = htons(SERVER_PORT);// Set server port
  38.  
  39.     memset((char*)&clientAddress, 0, sizeof(clientAddress));
  40.     clientAddress.sin_family = AF_INET;             // set server address protocol family
  41.     clientAddress.sin_addr.s_addr = INADDR_ANY;     // use all available addresses of server
  42.     clientAddress.sin_port = htons(SERVER_PORT);
  43.  
  44.     // Create a socket
  45.     SOCKET clientSocket = socket(AF_INET,      // IPv4 address famly
  46.                                  SOCK_DGRAM,   // Datagram socket
  47.                                  IPPROTO_UDP); // UDP protocol
  48.  
  49.     // Check if socket creation succeeded
  50.     if (clientSocket == INVALID_SOCKET)
  51.     {
  52.         printf("Creating socket failed with error: %d\n", WSAGetLastError());
  53.         WSACleanup();
  54.         return 1;
  55.     }
  56.     iResult = bind(clientSocket,(SOCKADDR*)&clientAddress,sizeof(clientAddress));
  57.     if (iResult == SOCKET_ERROR)
  58.     {
  59.         printf("bind failed with error: %d\n", WSAGetLastError());
  60.         closesocket(clientSocket);
  61.         WSACleanup();
  62.         return 1;
  63.     }
  64.     unsigned long mode = 1;
  65.     iResult = ioctlsocket(clientSocket, FIONBIO, &mode);
  66.     if (iResult != NOERROR)
  67.         printf("ioctlsocket failed with error: %ld", iResult);
  68.  
  69.     while (1) {
  70.        
  71.         iResult = recvfrom(clientSocket,
  72.             dataBuffer,
  73.             BUFFER_SIZE,
  74.             0,
  75.             (SOCKADDR*)&serverAddress,
  76.             &sockAddrLen);
  77.         if (iResult != SOCKET_ERROR)
  78.         {
  79.             dataBuffer[iResult] = '\0';
  80.             printf("server sent: %s.\n", dataBuffer);
  81.         }
  82.         else {
  83.             if (WSAGetLastError() == WSAEWOULDBLOCK)
  84.             {
  85.                 printf("Enter message to send:\n");
  86.  
  87.                 // Read string from user into outgoing buffer
  88.                 gets_s(dataBuffer, BUFFER_SIZE);
  89.  
  90.                 // Send message to server
  91.                 iResult = sendto(clientSocket,                      // Own socket
  92.                     dataBuffer,                     // Text of message
  93.                     strlen(dataBuffer),             // Message size
  94.                     0,                                  // No flags
  95.                     (SOCKADDR *)&serverAddress,     // Address structure of server (type, IP address and port)
  96.                     sizeof(serverAddress));         // Size of sockadr_in structure
  97.  
  98.         // Check if message is succesfully sent. If not, close client application
  99.                 if (iResult == SOCKET_ERROR)
  100.                 {
  101.                     printf("sendto failed with error: %d\n", WSAGetLastError());
  102.                     closesocket(clientSocket);
  103.                     WSACleanup();
  104.                     return 1;
  105.                 }
  106.             }
  107.             else
  108.             {
  109.                 printf("recvfrom failed with error: %d\n", WSAGetLastError());
  110.                 continue;
  111.             }
  112.         }
  113.     }
  114.     // Only for demonstration purpose
  115.     printf("Press any key to exit: ");
  116.     _getch();
  117.  
  118.     // Close client application
  119.     iResult = closesocket(clientSocket);
  120.     if (iResult == SOCKET_ERROR)
  121.     {
  122.         printf("closesocket failed with error: %d\n", WSAGetLastError());
  123.         WSACleanup();
  124.         return 1;
  125.     }
  126.  
  127.     // Close Winsock library
  128.     WSACleanup();
  129.  
  130.     // Client has succesfully sent a message
  131.     return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement