Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::SocketClient Struct Reference

Use a SocketDescriptor as a client (example a TCP or UDP socket client). More...

#include <SocketDescriptor.h>

Public Member Functions

 SocketClient (const SocketDescriptor &socket)
 Constructs this SocketClient from a SocketDescriptor (already created with SocketDescriptor::create) More...
 
Result connect (StringView address, uint16_t port)
 Connect to a given address and port combination. More...
 
Result connect (SocketIPAddress ipAddress)
 Connect to a given address and port combination. More...
 
Result write (Span< const char > data)
 Writes bytes to this socket. More...
 
Result read (Span< char > data, Span< char > &readData)
 Read bytes from this socket blocking until they're actually received. More...
 
Result readWithTimeout (Span< char > data, Span< char > &readData, Time::Milliseconds timeout)
 Read bytes from this socket blocking until they're actually received or timeout occurs. More...
 

Detailed Description

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):

SocketDescriptor acceptedClientSocket;
// ... assuming to obtain a TCP socket using SocketServer::accept
SC_TRY(server.accept(family, acceptedClientSocket));
SC_TRY(acceptedClientSocket.isValid());
// Read some data blocking until it's available
char buf[256];
SocketClient acceptedClient(acceptedClientSocket);
Span<char> readData;
SC_TRY(acceptedClient.read({buf, sizeof(buf)}, readData));
// ... later on
// Read again blocking but with a timeout of 10 seconds
SC_TRY(acceptedClient.readWithTimeout({buf, sizeof(buf)}, readData, Time::Milliseconds(10000)));
// Close the client
SC_TRY(acceptedClientSocket.close());
#define SC_TRY(expression)
Checks the value of the given expression and if failed, returns this value to caller.
Definition: Result.h:48
SocketClient(const SocketDescriptor &socket)
Constructs this SocketClient from a SocketDescriptor (already created with SocketDescriptor::create)
Definition: SocketDescriptor.h:238

Example (connecting client to server, doing two synchronous writes):

// ...assuming there is a socket listening at given serverAddress and tcpPort
SocketDescriptor clientSocket;
SocketClient client(clientSocket);
// Create a (TCP) socket
SC_TRY(clientSocket.create(family));
// [Alternatively] Create an UDP socket instead
// SC_TRY(clientSocket.create(family, SocketFlags::SocketDgram, SocketFlags::ProtocolUdp));
// Connect to the server
SC_TRY(client.connect(serverAddress, tcpPort));
// Write some data to the socket
const int testValue = 1;
char buf[1] = {testValue};
SC_TRY(client.write({buf, sizeof(buf)}));
buf[0]++; // change the value and write again
SC_TRY(client.write({buf, sizeof(buf)}));
// Close the socket
SC_TRY(clientSocket.close());

Constructor & Destructor Documentation

◆ SocketClient()

SC::SocketClient::SocketClient ( const SocketDescriptor socket)
inline

Constructs this SocketClient from a SocketDescriptor (already created with SocketDescriptor::create)

Parameters
socketA socket descriptor created with SocketDescriptor::create to be used as client

Member Function Documentation

◆ connect() [1/2]

Result SC::SocketClient::connect ( SocketIPAddress  ipAddress)

Connect to a given address and port combination.

Parameters
ipAddressAddress and port to connect to
Returns
Valid Result if this client successfully connected to the specified address and port

◆ connect() [2/2]

Result SC::SocketClient::connect ( StringView  address,
uint16_t  port 
)

Connect to a given address and port combination.

Parameters
addressAddress as string
portPort to start listening to
Returns
Valid Result if this client successfully connected to the specified address and port
Note
Socket descriptor MUST have already been created with SocketDescriptor::create

◆ read()

Result SC::SocketClient::read ( Span< char >  data,
Span< char > &  readData 
)

Read bytes from this socket blocking until they're actually received.

Parameters
[in]dataSpan of memory pointing at a buffer that will receive the read data
[out]readDataA sub-Span of data that has the length of actually read bytes
Returns
Valid Result if bytes have been read successfully

◆ readWithTimeout()

Result SC::SocketClient::readWithTimeout ( Span< char >  data,
Span< char > &  readData,
Time::Milliseconds  timeout 
)

Read bytes from this socket blocking until they're actually received or timeout occurs.

Parameters
[in]dataSpan of memory pointing at a buffer that will receive the read data
[out]readDataA sub-Span of data that has the length of actually read bytes
[in]timeoutFor how many milliseconds the read should wait before timing out
Returns
Valid Result if bytes have been read successfully and timeout didn't occur

◆ write()

Result SC::SocketClient::write ( Span< const char >  data)

Writes bytes to this socket.

Parameters
dataBytes to write to this socket
Returns
Valid Result if bytes have been written successfully

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