Skip to content

ATcpSocket#

A bidirectional TCP connection (either a client connection or returned by ATcpServerSocket).

Header:#include <AUI/Network/ATcpSocket.h>
CMake:aui_link(my_target PUBLIC aui::network)

Detailed Description#

ATcpSocket is both IInputStream and IOutputStream, so it plugs into everything that works with streams: AByteBuffer, AJson::fromStream, etc.

The constructor connects to the destination address and blocks until the connection is established or fails (throws AIOException).

ATcpSocket socket(AInet4Address::fromString("127.0.0.1", 8080));
socket.write("hello", 5);
char buf[256];
size_t n = socket.read(buf, sizeof(buf));

read blocks until some data arrives and returns the amount of bytes read, which can be less than requested. It returns 0 when the peer has closed the connection gracefully. TCP is a stream, not a message protocol: one write on one side does not correspond to one read on the other. Use IInputStream::readExact or a length prefix when you need whole messages.

See TCP client and server for a full client/server walkthrough.