UDP Manual

About

The User Datagram Protocol allows to send messages across networks without connections. The maxon::NetworkUdpInterface is used to create a sender to send UDP messages or to create a server to receive messages.

Note
The UDP interface uses an AioService. See maxon::AioServiceInterface.

NetworkUdpInterface

The maxon::NetworkUdpInterface provides these functions:

NetworkUdpServerInterface

The maxon::NetworkUdpServerInterface controls an UDP server that can receive UDP data:

// This example starts an UDP server. The behaviour on handling
// incoming data is defined with the given callback function.
// create AioServiceRef
g_ioService = maxon::AioServiceRef::Create() iferr_return;
g_ioService.Start() iferr_return;
// create and start server
g_udpServer = maxon::NetworkUdpInterface::CreateUdpServer(localAddr, UDPCallback, g_ioService) iferr_return;
g_udpServer.Start() iferr_return;
// This example shows a callback function that defines the behaviour of an UDP server for handling incoming data.
static maxon::Result<void> UDPCallback(maxon::Result<void> result, maxon::AioBuffer receivedData, maxon::NetworkIpAddrPort sender)
{
// print the remote address
DiagnosticOutput("Sender: @", sender);
// print the received data
const maxon::String message { receivedData };
DiagnosticOutput("Message: @", message);
return maxon::OK;
}

NetworkUdpSenderInterface

The maxon::NetworkUdpSenderInterface allows to send data via UDP to a remote machine.

// This example sends some data to a remote address using UDP.
// prepare AioServiceRef
const maxon::AioServiceRef service = maxon::AioServiceRef::Create() iferr_return;
service.Start() iferr_return;
// create sender
const maxon::NetworkUdpSenderRef sender = maxon::NetworkUdpInterface::CreateUdpSender(maxon::PROTOCOL::IPV4, service) iferr_return;
// prepare message
maxon::CString messageCStr("Test Message");
maxon::AioBuffer buffer;
for (const maxon::Char c : messageCStr)
{
iferr (buffer.Append(c))
err.DbgStop();
}
// send message
sender.Send(std::move(buffer), remoteAdress) iferr_return;

NetworkUdpChannelInterface

maxon::NetworkUdpChannelInterfac represents an asynchronous UDP channel that can both receive data and send data:

// This example creates an UDP channel that allows to send and receive data.
// The behaviour on receiving data is defined with the given callback.
// prepare AioServiceRef
g_channelIoService = maxon::AioServiceRef::Create() iferr_return;
g_channelIoService.Start() iferr_return;
// create channel
g_udpChannel = maxon::NetworkUdpInterface::CreateUdpChannel(localAddr, g_channelIoService) iferr_return;
g_udpChannel.Receive(ChannelReceiveCallback) iferr_return;
// This callback function for an UDP channel prints the received message.
{
// print the remote address
DiagnosticOutput("Sender: @", sender);
// print the received data
const maxon::String message { buf };
DiagnosticOutput("Message: @", message);
return maxon::OK;
}

Further Reading