Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SC::SocketServer Struct Reference

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

#include <Socket.h>

Public Member Functions

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

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

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

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

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 ( )
nodiscard

◆ listen()

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

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: