ADTF  3.18.2
cServerSocket

TCP Server socket class. More...

Inheritance diagram for cServerSocket:
[legend]

Public Types

enum  tOpenMode { SS_Exclusive = 1 }
 
- Public Types inherited from cSocket
enum  tOpenMode { OM_Block = 0x0 , OM_NonBlock = 0x01 }
 The mode in which to run the sockets during and after a connection. More...
 
enum  tShutdownMode { SM_Read = 0 , SM_Write = 1 , SM_ReadWrite = 2 }
 The mode in which to switch the sockets to on shutdown. More...
 
enum  tOptionLevel { OL_SOCKET = 1 , OL_IPPROTO_TCP = 2 , OL_IPPROTO_IP = 3 }
 Abstraction of the system specific socket option levels (e.g. More...
 
enum  tOptionId {
  OI_DEBUG , OI_BROADCAST , OI_REUSEADDR , OI_KEEPALIVE ,
  OI_LINGER , OI_SNDBUF , OI_RCVBUF , OI_DONTROUTE ,
  OI_TCP_NODELAY , OI_IP_MULTICAST_LOOP
}
 Abstraction of the system specific socket options (e.g. More...
 

Public Member Functions

 cServerSocket ()
 Default constructor.
 
virtual ~cServerSocket ()
 Destructor.
 
tResult Open (tInt nPort, tUInt32 dwMode=0, tUInt32 ui32InterfaceAddress=0)
 Opens the socket. More...
 
tResult Listen (tInt nMaxRequests=0)
 Puts the socket in listen mode. More...
 
tResult Accept (cStreamSocket &oStreamSocket)
 Accepts an incoming connection. More...
 
tResult Accept (tSocketHandle &hClientSocket)
 Accepts an incoming connection. More...
 
tBool IsConnected (tTimeStamp nTimeout=0)
 Polls the socket for its connection state. More...
 
- Public Member Functions inherited from cSocket
 cSocket ()
 Default constructor.
 
virtual ~cSocket ()
 Default destructor.
 
tVoid Close ()
 Closes the socket and frees all resources. More...
 
tVoid Close (tShutdownMode nHow)
 Closes the socket and frees all resources. More...
 
tUInt32 GetAddress () const
 Retrieves the IP address of the socket. More...
 
tInt GetPort () const
 Retrieves the port of the socket. More...
 
tSocketHandle GetHandle () const
 Get the internal socket handle. More...
 
tBool SetTimeout (tTimeStamp nReadTimeout=-1, tTimeStamp nWriteTimeout=-1)
 Sets timouts fo the read and write socket operations. More...
 
tBool DataAvailable ()
 Check if data is available for reading (a subsequent read succeed immediately). More...
 
tBool WaitForData (tTimeStamp nTimeout=-1)
 Blocks the current process until data is available for reading. More...
 
tInt SetOption (tInt nLevel, tInt nOptionId, const tVoid *pValue, tInt nValueLen)
 Sets a specific option for the socket. More...
 
tInt SetOption (cSocket::tOptionLevel eLevel, cSocket::tOptionId eId, const tVoid *pValue, tInt nValueLen)
 Equivalent to SetOption (tInt, tInt, const tVoid*, tInt) Instead of the system specific constants (e.g. More...
 
tResult GetOption (tInt nLevel, tInt nOptionId, tVoid *o_pValue, tInt *o_pValueLen)
 gets a specific option for the socket. More...
 
tResult GetOption (cSocket::tOptionLevel eLevel, cSocket::tOptionId eId, tVoid *o_pValue, tInt *o_pValueLen)
 Equivalent with GetOption(tInt, tInt, tVoid*, tInt*). More...
 

Additional Inherited Members

- Static Public Member Functions inherited from cSocket
static tVoid AddLibraryRef ()
 for the work with the static methods you have to use this function to load the nessessary libs, after work ReleaseLibraryRef has to be called.
 
static tVoid ReleaseLibraryRef ()
 to correctly unload the socket-libs you have to call this function
 
static cString GetHostName (tUInt32 dwHostAddress=0)
 Returns the hostname for a given address. More...
 
static tUInt32 GetHostAddress (const cString &strHostName=cString())
 Returns the address for a given hostname. More...
 
static tUInt32 AddressToUInt32 (const cString &strAddress)
 Converts a aaa.bbb.ccc.ddd formated address string to an integer. More...
 
static cString UInt32ToAddress (tUInt32 ui32Address)
 Converts an integer to an aaa.bbb.ccc.ddd formated address string. More...
 
static tInt GetLastErrorCode ()
 Gets the error code of the last failure. More...
 
static tResult GetLocalAddresses (std::set< tUInt32, std::less< tUInt32 >, std::allocator< tUInt32 > > &oAddresses)
 Returns a list of addresses from all local interfaces. More...
 
static tResult SocketError (tInt nSocketError)
 Maps a socket error to an A_UTILS error. More...
 
static tResult FindInterfaceForAddress (tUInt32 ui32FarAddress, tUInt32 &ui32LocalAddress)
 If more than one network interface is used in the host the FindInterfaceForAddress finds out the local address of the interface where the far address is reachable. More...
 
static tResult WaitForData (const std::list< cSocket * > &i_lstSockets, cSocket **o_ppSocket, tTimeStamp tmTimeOut=-1)
 Blocks the current thread until data are available for reading in at least one of i_lstSockets. More...
 
- Static Public Attributes inherited from cSocket
static const tUInt32 SOCKET_NO_ADDRESS
 Constance for check No Address.
 
- Protected Member Functions inherited from cSocket
tBool Initialize ()
 Sets the params for the socket with 0/nullptr. More...
 
tInt ResolveOptionLevel (tOptionLevel level) const
 Resolve the option value of the socket. More...
 
tInt ResolveOptionId (tOptionId id) const
 Resolve the option id of the socket. More...
 
- Protected Attributes inherited from cSocket
tSocketHandle m_hSocket
 this is internal handle of the socket, only use it if you really need that!!
 
tUInt32 m_ui32Address
 Address of the socket (valid only if opened)
 
tInt m_nPort
 Port of the socket (valid only if opened)
 

Detailed Description

TCP Server socket class.

This class provides the server counterpart to the TCP client implementation of cStreamSocket

Definition at line 324 of file streamsocket.h.

Member Enumeration Documentation

◆ tOpenMode

enum tOpenMode
Enumerator
SS_Exclusive 

The default SO_REUSEADDR�option is not set when this is specified.

On windows the SO_EXCLUSIVEADDRUSE option is set additionally.

Definition at line 332 of file streamsocket.h.

Member Function Documentation

◆ Accept() [1/2]

tResult Accept ( cStreamSocket oStreamSocket)

Accepts an incoming connection.

Parameters
oStreamSocket[out] This will be set up to communicate with the client socket.
Return values
ERR_ACCESS_DENIEDThe socket is marked nonblocking and no connections are present to be accepted (EAGAIN or EWOULDBLOCK).
ERR_INVALID_HANDLEThe descriptor is invalid (EBADF).
ERR_CANCELEDA connection has been aborted (ECONNABORTED).
ERR_INVALID_ADDRESSThe addr argument is not in a writable part of the user address space (EFAULT).
ERR_INVALID_FLAGSSocket is not listening for connections, or addrlen is invalid, or invalid value in flags (EINVAL).
ERR_MEMORYThe per-process limit of open file descriptors has been reached or the system limit on the total number of open files has been reached or not enough free memory (EMFILE or ENFILE or ENOBUFS or ENOMEM).
ERR_BAD_DEVICEThe descriptor references a file, not a socket (ENOTSOCK).
ERR_NOT_SUPPORTEDThe referenced socket is not of type SOCK_STREAM or protocol error (ENOTSOCK or EPROTO).
ERR_FAILEDOther error detected.

◆ Accept() [2/2]

tResult Accept ( tSocketHandle hClientSocket)

Accepts an incoming connection.

Parameters
hClientSocket[out] This will be set up to communicate with the client socket.
Return values
ERR_ACCESS_DENIEDThe socket is marked nonblocking and no connections are present to be accepted (EAGAIN or EWOULDBLOCK).
ERR_INVALID_HANDLEThe descriptor is invalid (EBADF).
ERR_CANCELEDA connection has been aborted (ECONNABORTED).
ERR_INVALID_ADDRESSThe addr argument is not in a writable part of the user address space (EFAULT).
ERR_INVALID_FLAGSSocket is not listening for connections, or addrlen is invalid, or invalid value in flags (EINVAL).
ERR_MEMORYThe per-process limit of open file descriptors has been reached or the system limit on the total number of open files has been reached or not enough free memory (EMFILE or ENFILE or ENOBUFS or ENOMEM).
ERR_BAD_DEVICEThe descriptor references a file, not a socket (ENOTSOCK).
ERR_NOT_SUPPORTEDThe referenced socket is not of type SOCK_STREAM or protocol error (ENOTSOCK or EPROTO).
ERR_FAILEDOther error detected

◆ IsConnected()

tBool IsConnected ( tTimeStamp  nTimeout = 0)
virtual

Polls the socket for its connection state.

This is particularly useful when running the sockets in non-blocking mode and Accept() is required to return without blocking.

Note
: The current connection is being verified read direction only, implying that the remote client socket will immediately acknowledge the TCP handshake upon a socket accept(). In case of UDP, this soely implies a successfully opened datagram socket.
Parameters
nTimeoutThe timeout after which the connection is deemed to have failed.
Returns
tTrue if Accept() would return immediately and without blocking.
tFalse if no client connection is queued at the moment.

Reimplemented from cSocket.

◆ Listen()

tResult Listen ( tInt  nMaxRequests = 0)

Puts the socket in listen mode.

Parameters
nMaxRequests[in] The number of pending connection requests before connections get dropped.
Return values
ERR_INVALID_ADDRESSAnother socket is already listening on the same port (EADDRINUSE).
ERR_INVALID_HANDLEThe argument sockfd is not a valid descriptor (EBADF).
ERR_BAD_DEVICEThe argument sockfd is not a socket (ENOTSOCK).
ERR_NOT_SUPPORTEDThe socket is not of a type that supports the Listen() operation (EOPNOTSUPP).
ERR_FAILEDOther error detected.

◆ Open()

tResult Open ( tInt  nPort,
tUInt32  dwMode = 0,
tUInt32  ui32InterfaceAddress = 0 
)

Opens the socket.

Opens a listening socket.

Parameters
[in]nPortPort number of server socket to open
[in]dwModeopen mode see tOpenMode
[in]ui32InterfaceAddressif more than one interface card and ip address of the host use this one. if 0 the server socket take any
Return values
ERR_DEVICE_IOOther socket-related error.
ERR_UNEXPECTEDCould not set socket options.
ERR_FAILEDBinding failed.
Parameters
nPort[in] The port to listen on.
dwModeunused.
ui32InterfaceAddressThe interface to bind to (network byte order). If 0, any address will be used.
Return values
ERR_ACCESS_DENIEDPermission to create a socket of the specified type and/or protocol is denied (EACCES).
ERR_NOT_SUPPORTEDThe implementation does not support the specified address family or the protocol type or the specified protocol is not supported within this domain (EAFNOSUPPORT or EPROTONOSUPPORT).
ERR_INVALID_FLAGSUnknown protocol or protocol family not available (EINVAL).
ERR_MEMORYProcess file table overflow or the system limit on the total number of open files has been reached or insufficient memory is available (EMFILE or ENFILE or ENOBUFS or ENOMEM).