libBsdSockets
C++ Wrapper classes to the BSD Socket API
BsdSockets::ServerSocket Class Reference

Server Socket providing ability to bind, listen and accept connections. More...

#include <ServerSocket.h>

Inheritance diagram for BsdSockets::ServerSocket:
Collaboration diagram for BsdSockets::ServerSocket:

Public Types

typedef std::shared_ptr< ServerSocketPtr
 
- Public Types inherited from BsdSockets::Socket
typedef std::shared_ptr< SocketPtr
 

Public Member Functions

virtual ~ServerSocket ()
 
void bind () const
 
void listen (int backlog=128) const
 
Socket::Ptr accept () const
 
std::thread acceptInNewThread (std::function< void(Socket::Ptr)> toCall)
 
- Public Member Functions inherited from BsdSockets::Socket
virtual ~Socket ()
 
SelectResult select (int timeout_ms, bool checkRead, bool checkWrite, bool checkError)
 
short poll (int timeout_ms, short eventsToLookFor)
 
ssize_t send (const void *buffer, size_t length, int flags=0) const
 
ssize_t receive (void *buffer, size_t length, int flags=0) const
 
ssize_t blockingReceive (void *buffer, size_t length) const
 
void getSocketOption (int level, int optionName, void *optionValue, socklen_t *optionLength) const
 
void setSocketOption (int level, int optionName, const void *optionValue, socklen_t optionLength) const
 
int getLowLevelSocket () const
 
std::shared_ptr< AddressgetAddress () const
 

Static Public Member Functions

static ServerSocket::Ptr create (std::shared_ptr< Address > theAddress)
 
static ServerSocket::Ptr open (std::shared_ptr< Address > theAddress, int backlog=128, bool reuse=false)
 
- Static Public Member Functions inherited from BsdSockets::Socket
static Socket::Ptr create (std::shared_ptr< Address > theAddress, int existingLowLevelSocket)
 

Protected Member Functions

 ServerSocket (std::shared_ptr< Address > theAddress)
 
- Protected Member Functions inherited from BsdSockets::Socket
 Socket (std::shared_ptr< Address > theAddress)
 

Detailed Description

Server Socket providing ability to bind, listen and accept connections.

Datagram sockets may send/receive with the ServerSocket directly. Other Sockets use the accept() returned Sockets.

Definition at line 38 of file ServerSocket.h.

Member Typedef Documentation

◆ Ptr

Constructor & Destructor Documentation

◆ ~ServerSocket()

BsdSockets::ServerSocket::~ServerSocket ( )
virtual

Closes socket. Virtual destructor to support derived classes.

Definition at line 103 of file ServerSocket.cpp.

◆ ServerSocket()

BsdSockets::ServerSocket::ServerSocket ( std::shared_ptr< Address theAddress)
protected

Create ServerSocket to listen to the given address.

Parameters
theAddressto bind and listen to

Member Function Documentation

◆ accept()

Socket::Ptr BsdSockets::ServerSocket::accept ( ) const

Accept a connection on the Server Socket.

Returns
the Socket accepted

Definition at line 125 of file ServerSocket.cpp.

◆ acceptInNewThread()

std::thread BsdSockets::ServerSocket::acceptInNewThread ( std::function< void(Socket::Ptr)>  toCall)

Accept a connection on the Server Socket and pass it as the argument to the function-wrapper toCall.

Parameters
toCallfunction to call with new socket
Returns
thread created

Definition at line 130 of file ServerSocket.cpp.

◆ bind()

void BsdSockets::ServerSocket::bind ( ) const

Bind this Socket to the address

Definition at line 111 of file ServerSocket.cpp.

◆ create()

ServerSocket::Ptr BsdSockets::ServerSocket::create ( std::shared_ptr< Address theAddress)
static

Create a ServerSocket to theAddress. This still needs bind() and listen().

Parameters
theAddressto open, bind and listen the ServerSocket on
Returns
the socket
See also
listen() parameter backlog for more details
Examples
ServerSocketExamples.cpp.

Definition at line 87 of file ServerSocket.cpp.

◆ listen()

void BsdSockets::ServerSocket::listen ( int  backlog = 128) const

Listen to the Socket with the given backlog.

Parameters
backlogmaximum length for the queue of pending connections.
See also
listen() parameter backlog for more details

Definition at line 119 of file ServerSocket.cpp.

◆ open()

ServerSocket::Ptr BsdSockets::ServerSocket::open ( std::shared_ptr< Address theAddress,
int  backlog = 128,
bool  reuse = false 
)
static

Create and open a ServerSocket to theAddress.

Parameters
theAddressto open, bind and listen the ServerSocket on
backloglength before events error
reusetrue to allow reuse of address and port, or false for default for timeout before reuse
Returns
the socket
See also
listen() parameter backlog for more details

Definition at line 91 of file ServerSocket.cpp.


The documentation for this class was generated from the following files: