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 Types

enum class  BindReuseAddress : uint8_t {
  Disabled ,
  Enabled
}
 
enum class  BindStatus : uint8_t {
  None ,
  AddressInUse
}
 

Public Member Functions

 SocketServer (SocketDescriptor &socket)
 Build a SocketServer from a SocketDescriptor (already created with SocketDescriptor::create)
 
Result close ()
 Calls SocketDescriptor::close.
 
Result bind (const SocketAddress &nativeAddress, BindReuseAddress reuseAddress=BindReuseAddress::Enabled, BindStatus *outStatus=nullptr)
 Binds this socket to an address.
 
Result bind (SocketIPAddress nativeAddress, BindReuseAddress reuseAddress=BindReuseAddress::Enabled, BindStatus *outStatus=nullptr)
 IP-address compatibility overload for bind.
 
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.
 
Result accept (SocketDescriptor &newClient, SocketAddress *peerAddress=nullptr)
 Accepts a new client and optionally reports its peer address.
 

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() [1/2]

Result SC::SocketServer::accept ( SocketDescriptor & newClient,
SocketAddress * peerAddress = nullptr )

Accepts a new client and optionally reports its peer address.

◆ accept() [2/2]

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

Accepts a new client, blocking while waiting for it.

Parameters
[in]addressFamilyCompatibility parameter; the accepted descriptor family is inherited from the listener
[out]newClientThe SocketDescriptor that will be accepted
Returns
Valid Result if the socket has been successfully accepted

◆ bind() [1/2]

Result SC::SocketServer::bind ( const SocketAddress & nativeAddress,
BindReuseAddress reuseAddress = BindReuseAddress::Enabled,
BindStatus * outStatus = nullptr )

Binds this socket to an address.

Parameters
nativeAddressThe local address to bind
reuseAddressWhether SO_REUSEADDR should be set before binding
outStatusOptional detailed status for bind failures
Returns
Valid Result if this socket has successfully been bound

◆ bind() [2/2]

Result SC::SocketServer::bind ( SocketIPAddress nativeAddress,
BindReuseAddress reuseAddress = BindReuseAddress::Enabled,
BindStatus * outStatus = nullptr )

IP-address compatibility overload for bind.

◆ close()

Result SC::SocketServer::close ( )

Calls SocketDescriptor::close.

Returns
The Result of SocketDescriptor::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
Datagram sockets cannot be listened. Stream sockets need a successful bind before listen

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