CppServer  1.0.4.0
C++ Server Library
udp_echo_server.cpp

UDP echo server example

#include "asio_service.h"
#include <iostream>
class EchoServer : public CppServer::Asio::UDPServer
{
public:
protected:
void onStarted() override
{
// Start receive datagrams
}
void onReceived(const asio::ip::udp::endpoint& endpoint, const void* buffer, size_t size) override
{
std::string message((const char*)buffer, size);
std::cout << "Incoming: " << message << std::endl;
// Echo the message back to the sender
SendAsync(endpoint, message);
}
void onSent(const asio::ip::udp::endpoint& endpoint, size_t sent) override
{
// Continue receive datagrams
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Echo UDP server caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
int main(int argc, char** argv)
{
// UDP server port
int port = 3333;
if (argc > 1)
port = std::atoi(argv[1]);
std::cout << "UDP server port: " << port << std::endl;
std::cout << std::endl;
// Create a new Asio service
auto service = std::make_shared<AsioService>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new UDP echo server
auto server = std::make_shared<EchoServer>(service, port);
// Start the server
std::cout << "Server starting...";
server->Start();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the server or '!' to restart the server..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Restart the server
if (line == "!")
{
std::cout << "Server restarting...";
server->Restart();
std::cout << "Done!" << std::endl;
continue;
}
}
// Stop the server
std::cout << "Server stopping...";
server->Stop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
virtual void onStarted()
Handle server started notification.
Definition: udp_server.h:310
virtual void ReceiveAsync()
Receive datagram from the client (asynchronous)
Definition: udp_server.cpp:530
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
Definition: udp_server.h:342
virtual void onSent(const asio::ip::udp::endpoint &endpoint, size_t sent)
Handle datagram sent notification.
Definition: udp_server.h:334
virtual bool SendAsync(const asio::ip::udp::endpoint &endpoint, const void *buffer, size_t size)
Send datagram into the given endpoint (asynchronous)
Definition: udp_server.cpp:349
UDPServer(const std::shared_ptr< Service > &service, int port, InternetProtocol protocol=InternetProtocol::IPv4)
Initialize UDP server with a given Asio service and port number.
Definition: udp_server.cpp:14
virtual void onReceived(const asio::ip::udp::endpoint &endpoint, const void *buffer, size_t size)
Handle datagram received notification.
Definition: udp_server.h:323
UDP server definition.