AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::network::TcpServer Class Reference

TCP server: accept connections as TcpStream. More...

#include <tcpServer.hpp>

Collaboration diagram for anch::network::TcpServer:

Public Member Functions

 TcpServer (const std::string &host, uint16_t port, int backlog=SOMAXCONN)
anch::network::TcpStream accept (std::size_t bufSize=anch::network::TcpStream::DEFAULT_BUFFER_SIZE)
anch::network::Socket acceptSocket (anch::network::Address *peer=NULL)
anch::network::Address getAddress () const
anch::network::Socket & getSocket () noexcept

Detailed Description

TCP server: accept connections as TcpStream.

anch::network::TcpServer server("", 40099);
while(running) {
anch::network::TcpStream peer = server.accept();
// handle peer (or move server.acceptSocket() to another thread and build the stream there)
}
Author
Vincent Lachenal
Since
0.2

Constructor & Destructor Documentation

◆ TcpServer()

anch::network::TcpServer::TcpServer ( const std::string & host,
uint16_t port,
int backlog = SOMAXCONN )

TcpServer constructor: bind and listen

Parameters
hostthe local host name or numeric address (empty for any address)
portthe local port (0 for an ephemeral port: see getAddress )
backlogthe maximum number of pending connections
Exceptions
anch::network::IOExceptionbind or listen error

Member Function Documentation

◆ accept()

anch::network::TcpStream anch::network::TcpServer::accept ( std::size_t bufSize = anch::network::TcpStream::DEFAULT_BUFFER_SIZE)

Wait for a connection

Parameters
bufSizethe stream buffer size
Returns
the connection stream
Exceptions
anch::network::IOExceptionaccept error

◆ acceptSocket()

anch::network::Socket anch::network::TcpServer::acceptSocket ( anch::network::Address * peer = NULL)

Wait for a connection (the socket can be moved to another thread, and a TcpStream built from it)

Parameters
peerif not NULL , receives the peer address
Returns
the connected socket
Exceptions
anch::network::IOExceptionaccept error

◆ getAddress()

anch::network::Address anch::network::TcpServer::getAddress ( ) const

Local address getter (useful for ephemeral port)

Returns
the address
Exceptions
anch::network::IOExceptionerror on getsockname

◆ getSocket()

anch::network::Socket & anch::network::TcpServer::getSocket ( )
noexcept

Listening socket getter

Returns
the socket

The documentation for this class was generated from the following file: