UDP Manual

内容表

关于

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.

注意
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 auto localAddr = maxon :: NetworkIpAddrPort ( maxon :: WILDCARD_IPV4_ADDRESS , 1000); 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) { iferr_scope ;

// 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 auto localAddr = maxon :: NetworkIpAddrPort ( maxon :: WILDCARD_IPV4_ADDRESS , 1001); 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. static maxon::Result<void> ChannelReceiveCallback( maxon::Result<void> , maxon::AioBuffer buf, maxon::NetworkIpAddrPort sender) { iferr_scope ;

// print the remote address DiagnosticOutput ( "Sender: @" , sender);

// print the received data const maxon::String message { buf }; DiagnosticOutput ( "Message: @" , message); return maxon::OK ; }

延伸阅读

maxon
The maxon namespace contains all declarations of the MAXON API.
定义: c4d_basedocument.h:15
maxon::String
定义: string.h:1197
maxon::OK
return OK
定义: apibase.h:2532
iferr_return
#define iferr_return
定义: resultbase.h:1434
maxon::BaseArray< Char >
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
IPV4
IPV4
IPv4.
定义: ge_prepass.h:5181
maxon::AioBuffer
BaseArray< Char > AioBuffer
定义: network_ioservice.h:16
PROTOCOL
PROTOCOL
定义: ge_prepass.h:5271
maxon::Result< void >
maxon::NetworkIpAddrPort
定义: network_ip_addr.h:438
iferr_scope
#define iferr_scope
定义: resultbase.h:1343
maxon::WILDCARD_IPV4_ADDRESS
static const WILDCARD_IPV4_ADDRESS_TYPE WILDCARD_IPV4_ADDRESS
定义: network_ip_addr.h:45
iferr
#define iferr(...)
定义: errorbase.h:380
Char
maxon::Char Char
定义: ge_sys_math.h:54

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1