Base Framework
Public Member Functions | List of all members
ServerSocket Class Reference

Server socket. More...

#include <base/net/ServerSocket.h>

Inherits Socket.

Public Member Functions

 ServerSocket () noexcept
 
 ServerSocket (const ServerSocket &copy) noexcept
 
 ServerSocket (const InetAddress &address, unsigned short port, unsigned int backlog)
 
 ServerSocket (const InetEndPoint &endPoint, unsigned int backlog)
 
StreamSocket accept ()
 
StreamSocket accept (unsigned int milliseconds)
 
void close ()
 
void getName () noexcept
 
const InetAddressgetLocalAddress () const noexcept
 
unsigned short getLocalPort () const noexcept
 
InetEndPoint getLocalEndPoint () const noexcept
 
void setNonBlocking (bool value)
 
void wait () const
 
bool wait (unsigned int microseconds) const
 

Additional Inherited Members

- Protected Types inherited from Socket
enum  { OPTION_DONT_ROUTE, OPTION_TCP_NO_DELAY, OPTION_TCP_DEFER_ACCEPT }
 
enum  Domain { IPV4, IPV6, DEFAULT_DOMAIN = IPV4 }
 
enum  Kind { STREAM, DATAGRAM, RAW }
 
- Protected Member Functions inherited from Socket
OperatingSystem::Handle getHandle () const noexcept
 
bool getBooleanOption (int option) const
 
void setBooleanOption (int option, bool value)
 
 Socket () noexcept
 
Domain getDomain () const
 
Kind getKind () const
 
bool accept (Socket &socket)
 
void bind (const InetAddress &address, unsigned short port)
 
void bind (const InetEndPoint &endPoint)
 
void close ()
 
void connect (const InetAddress &address, unsigned short port)
 
void connect (const InetEndPoint &endPoint)
 
void create (Kind kind, Domain domain=DEFAULT_DOMAIN)
 
void getName () noexcept
 
const InetAddressgetAddress () const noexcept
 
unsigned short getPort () const noexcept
 
InetEndPoint getEndPoint () const noexcept
 
const InetAddressgetLocalAddress () const noexcept
 
unsigned short getLocalPort () const noexcept
 
InetEndPoint getLocalEndPoint () const noexcept
 
void listen (unsigned int backlog)
 
void shutdownInputStream ()
 
void shutdownOutputStream ()
 
int getErrorState () const
 
bool getReuseAddress () const
 
void setReuseAddress (bool value)
 
bool getKeepAlive () const
 
void setKeepAlive (bool value)
 
bool getBroadcast () const
 
void setBroadcast (bool value)
 
int getLinger () const
 
void setLinger (int seconds)
 
int getReceiveBufferSize () const
 
void setReceiveBufferSize (int size)
 
int getSendBufferSize () const
 
void setSendBufferSize (int size)
 
bool getDontRoute () const
 
void setDontRoute (bool value)
 
uint64 getReceiveTimeout () const
 
void setReceiveTimeout (uint64 nanoseconds)
 
uint64 getSendTimeout () const
 
void setSendTimeout (uint64 nanoseconds)
 
bool getTcpNoDelay () const
 
void setTcpNoDelay (bool value)
 
uint64 getTcpDeferAccept () const
 
void setTcpDeferAccept (uint64 nanoseconds)
 
unsigned int getTimeToLive () const
 
void setTimeToLive (unsigned int value)
 
uint8 getMulticastHops () const
 
void setMulticastHops (uint8 value)
 
bool getMulticastLoopback () const
 
void setMulticastLoopback (bool value)
 
InetAddress getMulticastInterface () const
 
void setMulticastInterface (const InetAddress &interface)
 
uint8 getUnicastHops () const
 
void setUnicastHops (uint8 value)
 
void joinGroup (const InetAddress &group)
 
void joinGroup (const InetAddress &interface, const InetAddress &group)
 
void leaveGroup (const InetAddress &interface, const InetAddress &group)
 
bool getIPv6Restriction () const
 
void setIPv6Restriction (bool value)
 
void setNonBlocking (bool value)
 
unsigned int available () const
 
void flush ()
 
unsigned int read (uint8 *buffer, unsigned int size, bool nonblocking=false)
 
unsigned int write (const uint8 *buffer, unsigned int size, bool nonblocking=false)
 
unsigned int sendTo (const uint8 *buffer, unsigned int size, const InetAddress &address, unsigned short port)
 
unsigned int receiveFrom (uint8 *buffer, unsigned int size, InetAddress &address, unsigned short &port)
 
void asyncCancel ()
 
AsynchronousReadOperation read (uint8 *buffer, unsigned int bytesToRead, AsynchronousReadEventListener *listener)
 
AsynchronousWriteOperation write (const uint8 *buffer, unsigned int bytesToWrite, AsynchronousWriteEventListener *listener)
 
void wait () const
 
bool wait (unsigned int microseconds) const
 
 ~Socket ()
 
- Protected Member Functions inherited from Resource
String getDescription () const
 
void setDescription (const String &description)
 
unsigned int getResourceId () const
 
unsigned int getCreatedById () const
 
AnyReference getHandle () const noexcept
 
template<class TYPE >
TYPE & getInternalHandle () const
 
 operator bool () const noexcept
 
- Protected Member Functions inherited from AsynchronousStream
virtual ~AsynchronousStream () noexcept(false)
 
- Static Protected Member Functions inherited from Socket
static const char * toString (Domain domain) noexcept
 
static const char * toString (Kind kind) noexcept
 
- Static Protected Member Functions inherited from AsynchronousStream
static bool asyncTest () noexcept
 
- Protected Attributes inherited from Resource
AnyReference handle
 

Detailed Description

Server socket.

This class implements a stream server socket. A socket is an endpoint for communication between two hosts.

Version
1.0
Examples
testsuite/echod.cpp, testsuite/ftp.cpp, and testsuite/server.cpp.

Constructor & Destructor Documentation

◆ ServerSocket() [1/3]

ServerSocket::ServerSocket ( )
inlinenoexcept

Initializes an invalidated socket object (i.e. unconnected and unbound).

◆ ServerSocket() [2/3]

ServerSocket::ServerSocket ( const InetAddress address,
unsigned short  port,
unsigned int  backlog 
)

Creates a server stream socket and binds it to the specified port and IP address.

Parameters
addressThe IP address of the host to connect to.
portThe port to connect to on the host. If zero the socket is bound to a unique port.
backlogThe maxium length of the queue.

◆ ServerSocket() [3/3]

ServerSocket::ServerSocket ( const InetEndPoint endPoint,
unsigned int  backlog 
)

Creates a server stream socket and binds it to the specified port and IP address.

Parameters
endPointThe local end-point to connect to.
backlogThe maxium length of the queue.

Member Function Documentation

◆ accept() [1/2]

StreamSocket ServerSocket::accept ( )
inline

Accepts the first connection from the queue of pending connections on this socket. This function is used with a connection-oriented socket. This will block the calling thread for eternity if not connections are made.

Returns
True if connection has been accepted. False, if connection could not be accepted without blocking.
Examples
testsuite/ftp.cpp, and testsuite/server.cpp.

◆ accept() [2/2]

StreamSocket ServerSocket::accept ( unsigned int  milliseconds)
inline

Accepts the first connection from the queue of pending connections on this socket. This function is used with a connection-oriented socket.

Parameters
millisecondsThe maximum time to block.
Returns
An invalid socket if the timeout period expired.

◆ close()

void ServerSocket::close ( )
inline

Closes this socket.

Examples
testsuite/server.cpp.

◆ getLocalAddress()

const InetAddress& ServerSocket::getLocalAddress ( ) const
inlinenoexcept

Returns the local IP address to which the socket is bound.

Examples
testsuite/ftp.cpp, and testsuite/server.cpp.

◆ getLocalEndPoint()

InetEndPoint ServerSocket::getLocalEndPoint ( ) const
inlinenoexcept

Returns the local end-point to which the socket is bound.

◆ getLocalPort()

unsigned short ServerSocket::getLocalPort ( ) const
inlinenoexcept

Returns the local port to which the socket is bound.

Examples
testsuite/ftp.cpp, and testsuite/server.cpp.

◆ getName()

void ServerSocket::getName ( )
inlinenoexcept

Caches the locally assigned address and port of the socket. This member function can be used after a succesful accept determine the locally assigned address and port if unspecified.

Examples
testsuite/ftp.cpp.

◆ setNonBlocking()

void ServerSocket::setNonBlocking ( bool  value)
inline

Sets the blocking mode of the socket.

◆ wait() [1/2]

void ServerSocket::wait ( ) const
inline

Blocking wait for incoming connection request.

◆ wait() [2/2]

bool ServerSocket::wait ( unsigned int  microseconds) const
inline

Blocking wait for incoming connection request.

Parameters
microsecondsThe timeout periode in microseconds.
Returns
True, if incoming connection is available. False, if the timeout periode expired.