TCP sockets
On this page
AsyncTcpSocket4 and AsyncTcpSocket6 are TCP sockets
for asynchronous I/O through the core: the completion port on Windows, io_uring on Linux. Every operation comes in two forms: a
waiting form (Recv, Send, Accept, Connect, ...) that suspends the calling task until the
operation has completed, and a starting form (RecvAsync, SendAsync, ...) that never suspends
and leaves the waiting to you. Because a waiting task only suspends, a server can afford one task
per connection. The examples come from guide-tcp-sockets.cpp: an echo server on 127.0.0.1 with
an ephemeral port, and clients in the same process that check the echo.
Like Your first program, the program lets
main convert its thread to a task and start a scheduler, scheduler, before the parts shown
here. It uses these modules and names:
#include <ThinkMeta.ConcurrentTasks/Core.h>
import ThinkMeta.ConcurrentTasks.Tasks.Task;
import ThinkMeta.ConcurrentTasks.Synchronization.TaskGroup;
import ThinkMeta.ConcurrentTasks.AsyncIo.AsyncTcpSocket;
import ThinkMeta.ConcurrentTasks.Platform.Socket;
namespace TC = ThinkMeta::ConcurrentTasks;
using TC::AsyncIo::AsyncTcpSocket4;
using TC::Platform::SocketError; // WSA codes on Windows, errno on Linux
using TC::Platform::SocketErrorCode;
using ByteCount = AsyncTcpSocket4::ByteCount;Startup
int main()
{
// ...
// Windows: WSAStartup and the extension functions; nothing to do on Linux
if (SocketError::Failed(TC::Platform::Socket::Startup()))
return 1;
if (!TC::AsyncIo::EnableAsyncIo() || !TC::EnableTimers())
return 1;
// ...
}Socket::Startup calls WSAStartup for Winsock 2.2 on Windows and loads the
extension functions (AcceptEx, ConnectEx, DisconnectEx, ...) that the asynchronous operations
call through; on Linux it has nothing to do. Call it once per process before any socket is created,
and balance each successful call with Socket::Cleanup, so the same code runs
on both. The sockets also need EnableAsyncIo, and
EnableTimers if you use timeouts. On Windows link with ws2_32.lib.
Important
Create asynchronous sockets only through AsyncSocket::Create, which the
TCP classes' Create() calls. On Windows it creates an overlapped socket and associates it with
the core's completion port; a socket created any other way never sees its completions.
An accept loop with a task per connection
The server keeps a fixed number of connection tasks, and all tasks of the program join one task group:
constexpr int MaxConnections = 16; // connection tasks the server keeps
// all tasks of the program: server, connections, clients
TC::Synchronization::TaskGroup_s tasks;Each connection task echoes until the peer shuts down its sending side:
// One task per connection: echo everything until the peer shuts down its side.
class Connection : public TC::Tasks::Task
{
public:
AsyncTcpSocket4 socket; // filled by Accept
void Run() noexcept override
{
char buffer[16 * 1024];
for (;;) {
ByteCount received = 0;
SocketErrorCode error = socket.Recv(buffer, sizeof(buffer), &received);
if (SocketError::Failed(error) || received == 0)
break; // error, or 0 bytes: the peer has shut down sending
ByteCount sent = 0;
error = socket.Send(buffer, received, &sent);
if (SocketError::Failed(error) || sent != received)
break;
}
// no more data from us
socket.Shutdown(TC::Platform::SocketShutdown::Send);
socket.Close(); // nothing is pending: both waiting calls have returned
}
};A Recv that succeeds with 0 bytes means that the peer has closed or shut down its side of the connection. Each connection task owns its socket; the socket objects are movable, so you can also move an accepted socket into a task object.
The server task accepts a connection, hands the socket to a connection task and accepts the next one, until no client has connected for half a second:
// The accept loop: accept, hand the socket to a connection task, accept the next
// one. The server stops when all connection tasks are used or when no client
// connects for half a second.
class Server : public TC::Tasks::Task
{
public:
AsyncTcpSocket4 listener; // created, bound and listening before the task starts
Connection connections[MaxConnections];
void Run() noexcept override
{
for (Connection& connection : connections) {
const SocketErrorCode error = listener.Accept(
connection.socket, nullptr, TC::Duration::Milliseconds(500));
if (SocketError::Failed(error))
break; // SocketError::TimedOut, or another error
tasks.AddTask(&connection); // a task of the group may add tasks to it
connection.Schedule();
}
listener.Close();
}
};Accept takes an object that does not hold a socket yet and waits for
a connection. On Windows it creates the socket for the connection, waits with AcceptEx and then
sets SO_UPDATE_ACCEPT_CONTEXT, so that getpeername, shutdown and the other calls work on the
new socket; on Linux it takes over the socket that the kernel's accept returns. An optional pointer receives the peer's address. On failure the new socket is closed again.
Important
Connection tasks are ordinary tasks: the object must live from scheduling until its
OnTaskExited. The example keeps a fixed array of them and uses each once; a long-running server
needs a pool or another owner that reuses or frees a task object only after it has exited. See
Lifetimes and sizing.
Listening
main keeps the server and the client tasks in static objects and puts the server's socket into
listening state with ordinary socket calls:
int main()
{
// ...
static Server server; // large: keep it off the stack
static Client clients[Clients];
sockaddr_in address{};
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
address.sin_port = 0; // any free port
if (SocketError::Failed(server.listener.Create())
|| SocketError::Failed(server.listener.Bind(address))
|| SocketError::Failed(server.listener.GetSockName(address))
|| SocketError::Failed(server.listener.Listen()))
return 1;
// ...
}Create() without arguments creates a TCP socket of the version's address family.
Bind with port 0 picks a free port, which GetSockName
reports. Listen uses SOMAXCONN as the default backlog. These plain calls do
not suspend; a blocking socket call would block the OS thread, so use the asynchronous forms for
anything that waits.
Connecting, sending and receiving
On Windows Connect uses ConnectEx, which needs a bound socket: bind
it first, for example to port 0. It sets SO_UPDATE_CONNECT_CONTEXT after the connection. On Linux
a connect needs no bind, but one does no harm, so the example binds on both.
The waiting forms Send and Recv keep one operation in flight. The client below needs two at once: if it sent its whole message before reading, the server's sends could fill the client's receive buffer and both sides would wait for each other. So it starts the send with SendAsync and receives the echo while the send is in flight:
// Sends a message and receives the echo at the same time: the send runs as an
// asynchronous operation with its own request while the task waits in Recv.
bool Echo(const sockaddr_in& server, std::span<const char> message,
std::span<char> echo) noexcept
{
AsyncTcpSocket4 socket;
sockaddr_in local{};
local.sin_family = AF_INET;
local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
// Bound first: ConnectEx (Windows) needs it.
if (SocketError::Failed(socket.Create())
|| SocketError::Failed(socket.Bind(local)))
return false;
if (SocketError::Failed(socket.Connect(server, TC::Duration::Seconds(5))))
return false;
TC::AsyncIo::WaitableAsyncSocketIoRequest_s sendRequest;
ByteCount sent = 0;
const SocketErrorCode start = socket.SendAsync(
message.data(), static_cast<std::uint32_t>(message.size()), &sent,
sendRequest);
if (SocketError::Failed(start) && !SocketError::IsPending(start))
return false; // not started: never wait for this request
std::size_t received = 0;
while (received < echo.size()) {
ByteCount n = 0;
if (SocketError::Failed(
socket.Recv(echo.data() + received,
static_cast<std::uint32_t>(echo.size() - received), &n,
TC::Duration::Seconds(5)))
|| n == 0)
break;
received += n;
}
// Giving up: the send must not stay pending.
if (received < echo.size())
socket.CancelAllAsyncRequests();
sendRequest.Wait(); // the request must not go away while the send is pending
// the server's Recv returns 0 bytes now
socket.Shutdown(TC::Platform::SocketShutdown::Send);
return SocketError::Succeeded(sendRequest.GetError())
&& received == echo.size();
}The result of a starting call such as SendAsync decides what happens next:
| Result | Meaning |
|---|---|
SocketError::Success |
Completed synchronously; the request has already been completed on the calling thread. Windows only. |
SocketError::Pending |
In flight; the request completes later on the core's I/O thread. On Linux every started operation. |
| any other code | Failed synchronously; the request is not completed. Do not wait for it. |
If the echo does not arrive completely, Echo cancels the send before it waits for the request, so
that the wait cannot hang on a send that the server no longer reads.
Important
The request and every buffer passed to a starting call belong to you and must stay valid, and in place, until the request has completed. Reuse a request only after it has completed and after Reset. Several operations may be outstanding on one socket at once, each with its own request, started from any tasks or threads.
WaitableAsyncSocketIoRequest_s allows one waiting task and
Wait(Duration); once its Wait has returned it may be destroyed at once.
WaitableAsyncSocketIoRequest allows several waiting tasks, and
the Recv variants (WaitableAsyncSocketRecvIoRequest_s)
also capture the receive flags.
Running server and clients
Each client is a task that sends a message of its own through Echo and compares the echo:
class Client : public TC::Tasks::Task
{
public:
sockaddr_in server{};
int id = 0;
bool ok = false;
void Run() noexcept override
{
std::vector<char> message(MessageSize);
std::vector<char> echo(MessageSize);
for (std::size_t i = 0; i < MessageSize; ++i)
message[i] = static_cast<char>(i * (id + 3));
ok = Echo(server, message, echo) && echo == message;
}
};main starts the server task like any other task, then the clients with the address the listener
reported, and waits for the group:
int main()
{
// ...
tasks.AddTask(&server);
scheduler->AddTask(&server);
for (int i = 0; i < Clients; ++i) {
clients[i].server = address;
clients[i].id = i;
tasks.AddTask(&clients[i]);
scheduler->AddTask(&clients[i]);
}
tasks.Wait(); // server, connections and clients have exited
// ...
}Timeouts
Every waiting form takes an optional timeout (default Duration::Infinite).
When it expires, the operation is cancelled (CancelIoEx on Windows, the core's CancelIo on
Linux), the call waits until the cancellation has completed, and it returns SocketError::TimedOut
(WSAETIMEDOUT or ETIMEDOUT). If the operation completed before the cancellation took effect, its own result
stands. The server above uses this to stop once no client has connected for half a second.
A CancelTask of the waiting task ends the operation the same way, also with the
default Duration::Infinite(), and the call returns
SocketError::OperationAborted: the usual way to stop a task
that waits for data on shutdown (see Cancelling tasks).
Important
After a cancelled operation a stream socket is fit for closing only: data may have been lost. A finite timeout needs EnableTimers; without it the wait has no limit.
The Wait(Duration) of a request, by contrast, does not cancel anything: after it returns
WaitStatus::TimedOut or WaitStatus::Cancelled the operation is still pending and the request
must stay alive.
Shutting down and closing
- Shutdown
(SocketShutdown::Send)(SD_SENDon Windows,SHUT_WRon Linux) tells the peer that no more data follows; itsRecvthen returns 0 bytes. The clients and the connection tasks both use it. - Close closes the socket; the destructor does the same. Closing aborts pending
operations, which then complete with
SocketError::OperationAborted. - On Windows Disconnect uses
DisconnectEx; withTF_REUSE_SOCKETthe socket can be passed toAcceptAsyncorConnectAsyncagain.
Important
Close or destroy a socket only when no operation on it is pending: cancel them with CancelAsyncRequest or CancelAllAsyncRequests and wait for their completions first. Both only request the cancellation; the requests and buffers must stay alive until the operations have completed. At the end, the last DisableAsyncIo requires that no operation is pending in the whole process.
int main()
{
// ...
TC::DisableTimers();
TC::AsyncIo::DisableAsyncIo(); // all sockets are closed, nothing is pending
TC::Platform::Socket::Cleanup();
// ...
}Errors
All socket functions return a SocketErrorCode: 0 for success, otherwise a
WSA* code on Windows or an errno value on Linux; nothing throws.
SocketError names the common codes the same way on both platforms and offers
Succeeded, Failed and IsPending; note that Failed is also true for Pending. The waiting
forms set the byte count to 0 on failure. For callers who prefer exceptions,
WinsockErrorException wraps a code on Windows.
The program prints:
clients with a correct echo: 8 of 8
OK