MistServer  2.5.3-Pro-19-gf5e75b1 ( Generic_64)
Public Member Functions | Private Member Functions | Private Attributes
Socket::Server Class Reference

This class is for easily setting up listening socket, either TCP or Unix. More...

#include <socket.h>

Public Member Functions

 Server ()
 Create a new base Server. More...
 
 Server ()
 Create a new base Server. More...
 
 Server (int port, std::string hostname="0.0.0.0", bool nonblock=false)
 Create a new TCP Server. More...
 
 Server (int port, std::string hostname="0.0.0.0", bool nonblock=false)
 Create a new TCP Server. More...
 
 Server (std::string adres, bool nonblock=false)
 Create a new Unix Server. More...
 
 Server (std::string adres, bool nonblock=false)
 Create a new Unix Server. More...
 
Connection accept (bool nonblock=false)
 Accept any waiting connections. More...
 
Connection accept (bool nonblock=false)
 Accept any waiting connections. More...
 
void close ()
 Close connection. More...
 
void close ()
 Close connection. More...
 
bool connected () const
 Returns the connected-state for this socket. More...
 
bool connected () const
 Returns the connected-state for this socket. More...
 
void drop ()
 Close connection without shutdown. More...
 
void drop ()
 Close connection without shutdown. More...
 
int getSocket ()
 Returns internal socket number. More...
 
int getSocket ()
 Returns internal socket number. More...
 
bool isBlocking ()
 Check if this socket is blocking (true) or nonblocking (false). More...
 
bool isBlocking ()
 Check if this socket is blocking (true) or nonblocking (false). More...
 
void setBlocking (bool blocking)
 Set this socket to be blocking (true) or nonblocking (false). More...
 
void setBlocking (bool blocking)
 Set this socket to be blocking (true) or nonblocking (false). More...
 

Private Member Functions

bool IPv4bind (int port, std::string hostname, bool nonblock)
 Attempt to bind an IPv4 socket. More...
 
bool IPv4bind (int port, std::string hostname, bool nonblock)
 Attempt to bind an IPv4 socket. More...
 
bool IPv6bind (int port, std::string hostname, bool nonblock)
 Attempt to bind an IPv6 socket. More...
 
bool IPv6bind (int port, std::string hostname, bool nonblock)
 Attempt to bind an IPv6 socket. More...
 

Private Attributes

std::string errors
 Stores errors that may have occured. More...
 
int sock
 Internally saved socket number. More...
 

Detailed Description

This class is for easily setting up listening socket, either TCP or Unix.

Constructor & Destructor Documentation

Socket::Server::Server ( )

Create a new base Server.

Create a new base Server. The socket is never connected, and a placeholder for later connections.

Socket::Server::Server ( int  port,
std::string  hostname = "0.0.0.0",
bool  nonblock = false 
)

Create a new TCP Server.

The socket is immediately bound and set to listen. A maximum of 100 connections will be accepted between accept() calls. Any further connections coming in will be dropped.

Parameters
portThe TCP port to listen on
hostname(optional) The interface to bind to. The default is 0.0.0.0 (all interfaces).
nonblock(optional) Whether accept() calls will be nonblocking. Default is false (blocking).
Socket::Server::Server ( std::string  address,
bool  nonblock = false 
)

Create a new Unix Server.

The socket is immediately bound and set to listen. A maximum of 100 connections will be accepted between accept() calls. Any further connections coming in will be dropped. The address used will first be unlinked - so it succeeds if the Unix socket already existed. Watch out for this behaviour - it will delete any file located at address!

Parameters
addressThe location of the Unix socket to bind to.
nonblock(optional) Whether accept() calls will be nonblocking. Default is false (blocking).
Socket::Server::Server ( )

Create a new base Server.

Socket::Server::Server ( int  port,
std::string  hostname = "0.0.0.0",
bool  nonblock = false 
)

Create a new TCP Server.

Socket::Server::Server ( std::string  adres,
bool  nonblock = false 
)

Create a new Unix Server.

Member Function Documentation

Socket::Connection Socket::Server::accept ( bool  nonblock = false)

Accept any waiting connections.

If the Socket::Server is blocking, this function will block until there is an incoming connection. If the Socket::Server is nonblocking, it might return a Socket::Connection that is not connected, so check for this.

Parameters
nonblock(optional) Whether the newly connected socket should be nonblocking. Default is false (blocking).
Returns
A Socket::Connection, which may or may not be connected, depending on settings and circumstances.
Connection Socket::Server::accept ( bool  nonblock = false)

Accept any waiting connections.

void Socket::Server::close ( )

Close connection.

void Socket::Server::close ( )

Close connection.

The internal socket is closed and then set to -1. If the connection is already closed, nothing happens. This function calls shutdown, thus making the socket unusable in all other processes as well. Do not use on shared sockets that are still in use.

bool Socket::Server::connected ( ) const

Returns the connected-state for this socket.

Note that this function might be slightly behind the real situation. The connection status is updated after every accept attempt, when errors occur and when the socket is closed manually.

Returns
True if socket is connected, false otherwise.
bool Socket::Server::connected ( ) const

Returns the connected-state for this socket.

void Socket::Server::drop ( )

Close connection without shutdown.

void Socket::Server::drop ( )

Close connection without shutdown.

Close connection.

The internal socket is closed and then set to -1. If the connection is already closed, nothing happens. This function does not call shutdown, allowing continued use in other processes.

int Socket::Server::getSocket ( )

Returns internal socket number.

int Socket::Server::getSocket ( )

Returns internal socket number.

bool Socket::Server::IPv4bind ( int  port,
std::string  hostname,
bool  nonblock 
)
private

Attempt to bind an IPv4 socket.

bool Socket::Server::IPv4bind ( int  port,
std::string  hostname,
bool  nonblock 
)
private

Attempt to bind an IPv4 socket.

Parameters
portThe TCP port to listen on
hostnameThe interface to bind to. The default is 0.0.0.0 (all interfaces).
nonblockWhether accept() calls will be nonblocking. Default is false (blocking).
Returns
True if successful, false otherwise.
bool Socket::Server::IPv6bind ( int  port,
std::string  hostname,
bool  nonblock 
)
private

Attempt to bind an IPv6 socket.

Parameters
portThe TCP port to listen on
hostnameThe interface to bind to. The default is 0.0.0.0 (all interfaces).
nonblockWhether accept() calls will be nonblocking. Default is false (blocking).
Returns
True if successful, false otherwise.
bool Socket::Server::IPv6bind ( int  port,
std::string  hostname,
bool  nonblock 
)
private

Attempt to bind an IPv6 socket.

bool Socket::Server::isBlocking ( )

Check if this socket is blocking (true) or nonblocking (false).

bool Socket::Server::isBlocking ( )

Check if this socket is blocking (true) or nonblocking (false).

Set this socket to be blocking (true) or nonblocking (false).

void Socket::Server::setBlocking ( bool  blocking)

Set this socket to be blocking (true) or nonblocking (false).

void Socket::Server::setBlocking ( bool  blocking)

Set this socket to be blocking (true) or nonblocking (false).

Field Documentation

std::string Socket::Server::errors
private

Stores errors that may have occured.

int Socket::Server::sock
private

Internally saved socket number.


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