Advertisement
Vlad3955

userver.cpp

Oct 5th, 2022
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include "userver.h"
  2.  
  3.  
  4.  
  5.  
  6. UDPServer::UDPServer(unsigned short port)
  7.     : m_sock{ AF_INET, SOCK_DGRAM, IPPROTO_UDP }, m_client_address{ 0 }, m_client_address_len{ sizeof(sockaddr_in) }, m_port(port), m_recv_len{ 0 } {}
  8.  
  9. void UDPServer::listenUDP()
  10. {
  11.     std::cout << "Starting echo server on the port " << m_port << "...\n";
  12.  
  13.     if (!m_sock)
  14.     {
  15.         std::cerr << m_sock_wrap.get_last_error_string() << std::endl;
  16.         exit(EXIT_FAILURE);
  17.     }
  18.  
  19.  
  20.  
  21.     sockaddr_in addr
  22.     {
  23.         .sin_family = PF_INET,
  24.         .sin_port = htons(m_port),
  25.     };
  26.  
  27.     addr.sin_addr.s_addr = INADDR_ANY;
  28.  
  29.     if (bind(m_sock, reinterpret_cast<const sockaddr*>(&addr), sizeof(addr)) != 0)
  30.     {
  31.         std::cerr << m_sock_wrap.get_last_error_string() << std::endl;
  32.         // Socket will be closed in the Socket destructor.
  33.         exit(EXIT_FAILURE);
  34.     }
  35.  
  36.     std::cout << "Running echo server(Enter 'exit' to exit)...\n" << std::endl;
  37.     const int len = 256;
  38.     char buffer[len];
  39.     char client_address_buf[INET_ADDRSTRLEN];
  40.     char hbuf[NI_MAXHOST];
  41.     bool run = true;
  42.  
  43.     while (run)
  44.     {
  45.         // Read content into buffer from an incoming client.
  46.         m_recv_len = recvfrom(m_sock, buffer, sizeof(buffer) - 1, 0,
  47.             reinterpret_cast<sockaddr*>(&m_client_address),
  48.             &m_client_address_len);
  49.  
  50.  
  51.  
  52.         int x = 0;
  53.         if (m_recv_len > 0)
  54.         {
  55.             buffer[m_recv_len] = '\0';
  56.             std::cout
  57.                 << "Client with address: "
  58.                 << inet_ntop(AF_INET, &m_client_address.sin_addr, client_address_buf, sizeof(client_address_buf) / sizeof(client_address_buf[0]))
  59.                 << ":" << ntohs(m_client_address.sin_port)
  60.                 << " and host name: ";
  61.             if (x = getnameinfo(reinterpret_cast<sockaddr*>(&m_client_address), m_client_address_len,
  62.                 hbuf, sizeof(hbuf), nullptr, 0, NI_NAMEREQD) != 0)
  63.             {
  64.                 std::cerr << "could not resolve hostname" << std::endl;
  65.             }
  66.             else
  67.             {
  68.                 std::cout << "host" << hbuf << std::endl;
  69.             }
  70.             std::cout << " sent datagram "
  71.                 << "[length = "
  72.                 << m_recv_len
  73.                 << "]:\n'''\n"
  74.                 << buffer
  75.                 << "\n'''"
  76.                 << std::endl;
  77.             std::string command_string = { buffer, 0, len };
  78.             rtrim(command_string);
  79.             if ("exit" == command_string)
  80.             {
  81.                 std::cout << "See you!" << std::endl;
  82.                 run = false;
  83.             }
  84.  
  85.  
  86.  
  87.          // Send same content back to the client ("echo").
  88.             sendto(m_sock, buffer, m_recv_len, 0, reinterpret_cast<const sockaddr*>(&m_client_address),
  89.                 m_client_address_len);
  90.         }
  91.  
  92.         std::cout << std::endl;
  93.     }
  94. }
  95.  
  96. UDPServer::~UDPServer() {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement