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.Start() iferr_return;
// create and start server
g_udpServer = maxon::NetworkUdpInterface::CreateUdpServer(localAddr, UDPCallback, g_ioService) iferr_return;
g_udpServer.Start() iferr_return;
Definition: network_ip_addr.h:444
static auto Create(ARGS &&... args)
Definition: apibase.h:2773
The maxon namespace contains all declarations of the MAXON API.
Definition: autoweight.h:14
static const WILDCARD_IPV4_ADDRESS_TYPE WILDCARD_IPV4_ADDRESS
Definition: network_ip_addr.h:45
#define iferr_return
Definition: resultbase.h:1519
// This example shows a callback function that defines the behaviour of an UDP server for handling incoming data.
{
// print the remote address
DiagnosticOutput("Sender: @", sender);
// print the received data
const maxon::String message { receivedData };
DiagnosticOutput("Message: @", message);
return maxon::OK;
}
Definition: string.h:1235
PyObject PyObject * result
Definition: abstract.h:43
const char * message
Definition: pyerrors.h:189
return OK
Definition: apibase.h:2690
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:176
#define iferr_scope
Definition: resultbase.h:1384

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");
for (const maxon::Char c : messageCStr)
{
iferr (buffer.Append(c))
err.DbgStop();
}
// send message
sender.Send(std::move(buffer), remoteAdress) iferr_return;
const char ** buffer
Definition: abstract.h:327
Py_UNICODE c
Definition: unicodeobject.h:1200
for(i=0;i< length;i++)
Definition: unicodeobject.h:61
IPV4
IPv4.
Definition: ge_prepass.h:1
maxon::Char Char
Definition: ge_sys_math.h:56
PROTOCOL
Definition: ge_prepass.h:5548
#define iferr(...)
Definition: errorbase.h:388
BaseArray< Char > AioBuffer
Definition: network_ioservice.h:15

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.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
DiagnosticOutput("Message: @", message);
return maxon::OK;
}
void * buf
Definition: abstract.h:289

Further Reading