TCP Manual
The Transmission Control Protocol allows to create network connections to remote machines in order to send data. The maxon::NetworkTcpInterface is used to create either an outgoing connection to send TCP/IP data or to create an server to receive such data.
The maxon::NetworkTcpInterface provides these functions:
The maxon::NetworkTcpServerInterface defines a TCP server that reacts to incoming TCP connections:
// create AioService g_tcpIoService = maxon::AioServiceRef::Create() iferr_return ; g_tcpIoService.Start() iferr_return ;
// create and start server auto localAddr = maxon :: NetworkIpAddrPort ( maxon :: WILDCARD_IPV4_ADDRESS , 2000); g_tcpServer = maxon ::NetworkTcpInterface::CreateServer(localAddr, TCPServerCallback, g_tcpIoService, maxon :: JOBQUEUE_CURRENT ) iferr_return ; g_tcpServer.Start() iferr_return ;
For an example on the callback function see below.
The maxon::NetworkTcpConnectionInterface represents an established TCP connection. This connection can receive data from the remote machine or send data to that machine.
// print remote address const maxon::NetworkIpAddrPort remote = connection.GetRemoteAddress(); DiagnosticOutput ( "Connected with @" , remote);
// define receive callback connection.Receive( []( maxon::Result<void> res, maxon::AioBuffer buffer) -> maxon::Result<void> { iferr_scope ; // print received message const maxon::String message { buffer }; DiagnosticOutput ( "Message: @" , message); return maxon::OK ; }) iferr_return ; return maxon::OK ; }
// This example opens a TCP connection to a remote host and sends some data.
// prepare AioServiceRef const maxon::AioServiceRef service = maxon::AioServiceRef::Create() iferr_return ; service.Start() iferr_return ;
// open TCP connection maxon ::NetworkTcpInterface::OpenOutgoingConnection(remoteAddr, []( maxon ::Result< void >, maxon ::NetworkTcpConnectionRef connection) -> maxon ::Result< void > { iferr_scope ;
// prepare message maxon::CString messageCStr( "Test Message" ); maxon::AioBuffer buffer; for ( const maxon::Char c: messageCStr) { iferr (buffer. Append (c)) err.DbgStop(); }
// send message return connection.Send(std::move(buffer)); }, service, maxon::JOBQUEUE_CURRENT ) iferr_return ;