|
| | SocketClient (const SocketDescriptor &socket) |
| | Constructs this SocketClient from a SocketDescriptor (already created with SocketDescriptor::create)
|
| |
| Result | connect (StringSpan address, uint16_t port) |
| | Connect to a given address and port combination.
|
| |
| Result | connect (SocketIPAddress ipAddress) |
| | Connect to a given address and port combination.
|
| |
| Result | write (Span< const char > data) |
| | Writes bytes to this socket.
|
| |
| Result | read (Span< char > data, Span< char > &readData) |
| | Read bytes from this socket blocking until they're actually received.
|
| |
| Result | readWithTimeout (Span< char > data, Span< char > &readData, int64_t timeout) |
| | Read bytes from this socket blocking until they're actually received or timeout occurs.
|
| |
Use a SocketDescriptor as a client (example a TCP or UDP socket client).
The socket client can be obtained via SC::SocketServer::accept or connected to an endpoint through SC::SocketClient::connect.
Example (accepted client from server, doing a synchronous read):
SC_TRY(server.accept(family, acceptedClientSocket));
SC_TRY(acceptedClientSocket.isValid());
char buf[256];
Span<char> readData;
SC_TRY(acceptedClient.read({buf, sizeof(buf)}, readData));
SC_TRY(acceptedClient.readWithTimeout({buf, sizeof(buf)}, readData, 10 * 1000));
SC_TRY(acceptedClientSocket.close());
Example (connecting client to server, doing two synchronous writes):
SC_TRY(clientSocket.
create(family));
SC_TRY(client.connect(serverAddress, tcpPort));
const int testValue = 1;
char buf[1] = {testValue};
SC_TRY(client.write({buf, sizeof(buf)}));
buf[0]++;
SC_TRY(client.write({buf, sizeof(buf)}));
SC_TRY(clientSocket.close());