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

maxon
The maxon namespace contains all declarations of the MAXON API.
Definition: c4d_basedocument.h:27
maxon::String
Definition: string.h:1197
maxon::OK
return OK
Definition: apibase.h:2490
iferr_return
#define iferr_return
Definition: resultbase.h:1434
maxon::BaseArray< Char >
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:168
IPV4
IPV4
IPv4.
Definition: ge_prepass.h:5136
maxon::AioBuffer
BaseArray< Char > AioBuffer
Definition: network_ioservice.h:16
PROTOCOL
PROTOCOL
Definition: ge_prepass.h:5226
maxon::Result< void >
maxon::NetworkIpAddrPort
Definition: network_ip_addr.h:438
iferr_scope
#define iferr_scope
Definition: resultbase.h:1343
maxon::WILDCARD_IPV4_ADDRESS
static const WILDCARD_IPV4_ADDRESS_TYPE WILDCARD_IPV4_ADDRESS
Definition: network_ip_addr.h:45
iferr
#define iferr(...)
Definition: errorbase.h:380
Char
maxon::Char Char
Definition: ge_sys_math.h:41