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

Use a SocketDescriptor as a Server (example TCP or UDP Socket Server). More...

#include <SocketDescriptor.h>

Public Member Functions

 SocketServer (SocketDescriptor &socket)
 Build a SocketServer from a SocketDescriptor (already created with SocketDescriptor::create) More...
 
Result close ()
 Calls SocketDescriptor::close. More...
 
Result bind (SocketIPAddress nativeAddress)
 Binds this socket to a given address / port combination. More...
 
Result listen (uint32_t numberOfWaitingConnections)
 Start listening for incoming connections at a specific address / port combination (after bind) More...
 
Result accept (SocketFlags::AddressFamily addressFamily, SocketDescriptor &newClient)
 Accepts a new client, blocking while waiting for it. More...
 

Detailed Description

Use a SocketDescriptor as a Server (example TCP or UDP Socket Server).

Example:

SocketDescriptor serverSocket;
SocketServer server(serverSocket);
// Look for an available port
constexpr int tcpPort = 5050;
const StringView serverAddress = "::1"; // or "127.0.0.1"
SocketIPAddress nativeAddress;
SC_TRY(nativeAddress.fromAddressPort(serverAddress, tcpPort));
SocketFlags::AddressFamily family = nativeAddress.getAddressFamily();
// Create socket and start listening
SC_TRY(serverSocket.create(family)); // By default creates a TCP Server
// [Alternatively] Create an UDP socket instead
// SC_TRY(serverSocket.create(family, SocketFlags::SocketDgram, SocketFlags::ProtocolUdp));
SC_TRY(server.bind(nativeAddress)); // Bind the socket to the given address
SC_TRY(server.listen(1)); // Start listening (skip this for UDP sockets)
// Accept a client
SocketDescriptor acceptedClientSocket;
SC_TRY(server.accept(family, acceptedClientSocket));
SC_TRY(acceptedClientSocket.isValid());
// ... Do something with acceptedClientSocket
#define SC_TRY(expression)
Checks the value of the given expression and if failed, returns this value to caller.
Definition: Result.h:47
AddressFamily
Sets the address family of an IP Address (IPv4 or IPV6)
Definition: SocketDescriptor.h:84
SocketServer(SocketDescriptor &socket)
Build a SocketServer from a SocketDescriptor (already created with SocketDescriptor::create)
Definition: SocketDescriptor.h:197

Constructor & Destructor Documentation

◆ SocketServer()

SC::SocketServer::SocketServer ( SocketDescriptor socket)
inline

Build a SocketServer from a SocketDescriptor (already created with SocketDescriptor::create)

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

Member Function Documentation

◆ accept()

Result SC::SocketServer::accept ( SocketFlags::AddressFamily  addressFamily,
SocketDescriptor newClient 
)

Accepts a new client, blocking while waiting for it.

Parameters
[in]addressFamilyThe address family of the SocketDescriptor that will be created
[out]newClientThe SocketDescriptor that will be accepted
Returns
Valid Result if the socket has been successfully accepted

◆ bind()

Result SC::SocketServer::bind ( SocketIPAddress  nativeAddress)

Binds this socket to a given address / port combination.

Parameters
nativeAddressThe interface ip address and port to start listening to
Returns
Valid Result if this socket has successfully been bound

◆ close()

Result SC::SocketServer::close ( )

◆ listen()

Result SC::SocketServer::listen ( uint32_t  numberOfWaitingConnections)

Start listening for incoming connections at a specific address / port combination (after bind)

Parameters
numberOfWaitingConnectionsHow many connections can be queued before accept
Returns
Valid Result if this socket has successfully been put in listening mode
Note
UDP socket cannot be listened. TCP socket need a successful SocketServer::bind before SocketServer::listen

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