TCP Sockets

Detailed Description

TCP is a connected byte stream between two endpoints, implemented by two types: TcpServer listens on a local endpoint and reports pending peers, and TcpSocket is the stream that transfers bytes, whether it is the client side of a connect() or the accepted side of a pending server connection.

A server listen() binds the local endpoint and waits for peers. TcpSocket::accept() takes one pending connection and makes that socket the accepted stream, while TcpSocket::connect() reaches a remote endpoint and makes that socket the client stream. After the stream is up, read() and write() are the inherited I/O-device operations, blocking or asynchronous.

TcpServer is a Selectable, not an IODevice, so it does not read or write, whereas TcpSocket is an IODevice. Listen and connection settings live in TcpServerOptions and TcpSocketOptions, which configure an operation but do not open a socket.

The example is the two-type model on one thread: listen() queues incoming connections, connect() completes against that queue, accept() takes the pending stream, and the bytes then move through the inherited device operations.

peer.accept(server);
peer.write("Hello", 5);
char buf[5];
client.read(buf, 5);

Classes

class  TcpServerOptions
 TCP server listen options. More...
 
class  TcpServer
 Listens for TCP connections. More...
 
class  TcpSocketOptions
 TCP socket connection options. More...
 
class  TcpSocket
 Connected TCP byte stream. More...
 
Connected TCP byte stream.
Definition: TcpSocket.h:141
void accept(TcpServer &server)
Accepts a connection from server.
Listens for TCP connections.
Definition: TcpServer.h:159
std::size_t write(const char *buffer, std::size_t n)
Write data to I/O device.
std::size_t read(char *buffer, std::size_t n)
Read data from I/O device.
void connect(const Endpoint &ep)
Connects to ep.
void listen(const Endpoint &ep)
Listens on local endpoint ep.
static Endpoint ip4Any(unsigned short port)
Creates the IPv4 any-address for port.
static Endpoint ip4Loopback(unsigned short port)
Creates the IPv4 loopback address for port.