ServerSocket Class Reference

A server (listening) socket for StreamSocket (TCP) connections. More...

#include <ServerSocket.h++>

Inheritance diagram for ServerSocket:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 ServerSocket (uint16_t port, uint_t backlog=3) throw ()
 Construct a new ServerSocket that will listen on the given port.
 ServerSocket (uint16_t port, const NetworkInterface &ixface, uint_t backlog=3) throw ()
 Construct a new ServerSocket that will listen on the given port and network interface.
 ~ServerSocket () throw ()
 Destructor.
void init () throw (SocketException)
 Initialize the socket.
void shutdown () throw ()
 Shut down the socket.
void listen () throw (SocketException)
 Begin listening for connections on the socket.
bool isListening () const throw ()
 Determine if the socket is in a listening state.
void accept (StreamSocket &socket) throw (SocketException, TimeoutException)
 Accept a connection on the socket.
virtual void connect (const String &addr, uint16_t port) throw (IOException)
 Connect the socket to a remote endpoint.
virtual void connect (const SocketAddress &addr) throw (IOException)
 Connect the socket to a remote endpoint.
void setTimeout (timespan_ms_t timeout) throw (SocketException)
 Set the timeout for the socket.
void setReceiveBufSize (size_t size) throw (SocketException)
 Set the size of the socket's internal receive buffer.
size_t getReceiveBufSize () const throw (SocketException)
 Get the size of the socket's internal receive buffer.
void setSendBufSize (size_t size) throw (SocketException)
 Set the size of the socket's internal send buffer.
size_t getSendBufSize () const throw (SocketException)
 Get the size of the socket's internal send buffer.
void setLingerTime (timespan_s_t timeout) throw (SocketException)
 Set the linger time for the socket.
timespan_s_t getLingerTime () const throw (SocketException)
 Get the linger time for the socket.
void setReuseAddress (bool enable) throw (SocketException)
 Enable or disable the SO_REUSEADDR option on the socket.
bool getReuseAddress () const throw (SocketException)
 Determine if the SO_REUSEADDR option is enabled or disabled.
void setKeepAlive (bool enable) throw (SocketException)
 Enable or disable the SO_KEEPALIVE option on the socket.
bool getKeepAlive () throw (SocketException)
 Determine if the SO_KEEPALIVE option is enabled or disabled.
void setTCPDelay (bool enable) throw (SocketException)
 Enable or disable the Nagle algorithm for send coalescing.
bool getTCPDelay () throw (SocketException)
 Determine if the Nagle algorithm is enabled or disabled.
bool isInitialized () const throw ()
 Determine if the socket has been initialized.
bool isConnected () const throw ()
 Determine if the socket is connected.
NetProtocol getType () const throw ()
 Get the socket type.
const SocketAddressgetLocalAddress () const throw ()
 Get the address of the local end of the socket.
const SocketAddressgetRemoteAddress () const throw ()
 Get the address of the remote (peer) end of the socket.

Protected Types

enum  IOWaitMode { WaitWrite, WaitRead }

Protected Member Functions

SocketHandle getSocketHandle () const throw ()
 Get the underlying socket handle for this socket.
void waitForIO (IOWaitMode mode) throw (IOException)
 Wait for the socket to become ready for reading or writing.

Protected Attributes

NetProtocol _type
 The socket type.
SocketHandle _socket
 A handle to the socket itself.
SocketAddress _raddr
 The remote address.
SocketAddress _laddr
 The local address.
int _sotimeout
 The timeout value.

Detailed Description

A server (listening) socket for StreamSocket (TCP) connections.

Author:
Mark Lindner

Member Enumeration Documentation

enum IOWaitMode [protected, inherited]
Enumerator:
WaitWrite 
WaitRead 

Constructor & Destructor Documentation

ServerSocket ( uint16_t  port,
uint_t  backlog = 3 
) throw ()

Construct a new ServerSocket that will listen on the given port.

Parameters:
port The port number to listen on.
backlog The size of the connection backlog.
ServerSocket ( uint16_t  port,
const NetworkInterface ixface,
uint_t  backlog = 3 
) throw ()

Construct a new ServerSocket that will listen on the given port and network interface.

Parameters:
port The port number to listen on.
ixface The network interface to listen on.
backlog The size of the connection backlog.
~ServerSocket (  )  throw ()

Destructor.

Shuts down the socket.


Member Function Documentation

void accept ( StreamSocket socket  )  throw (SocketException, TimeoutException)

Accept a connection on the socket.

This method blocks until a new connection is pending, unless a timeout has been set on the socket, in which case a TimeoutException is thrown if no connection is pending by the time the timeout expires.

Parameters:
socket A socket object which will be initialized to represent the newly-established connection.
Exceptions:
TimeoutException If the operation times out.
SocketException If a socket error occurs.
void connect ( const SocketAddress addr  )  throw (IOException) [virtual, inherited]

Connect the socket to a remote endpoint.

Note that if this call fails, the socket will revert to an uninitialized state; call init() again to reuse the socket.

Parameters:
addr The address.
Exceptions:
IOException If an error occurs.

Reimplemented in DatagramSocket, and StreamSocket.

void connect ( const String addr,
uint16_t  port 
) throw (IOException) [virtual, inherited]

Connect the socket to a remote endpoint.

Note that if this call fails, the socket will revert to an uninitialized state; call init() again to reuse the socket.

Parameters:
addr The address, which may either be a dot-separated IP address or a DNS name.
port The port number.
Exceptions:
IOException If an error occurs.

Reimplemented in DatagramSocket, and StreamSocket.

bool getKeepAlive (  )  throw (SocketException) [inherited]

Determine if the SO_KEEPALIVE option is enabled or disabled.

Returns:
true if the option is enabled, false otherwise.
Exceptions:
SocketException If a socket error occurs.
timespan_s_t getLingerTime (  )  const throw (SocketException) [inherited]

Get the linger time for the socket.

Returns:
The linger time, in seconds, or -1 if the feature is disabled.
Exceptions:
SocketException If the linger time could not be retrieved.
const SocketAddress& getLocalAddress (  )  const throw () [inline, inherited]

Get the address of the local end of the socket.

size_t getReceiveBufSize (  )  const throw (SocketException) [inherited]

Get the size of the socket's internal receive buffer.

Returns:
The size, in bytes.
Exceptions:
SocketException If the size could not be changed.
const SocketAddress& getRemoteAddress (  )  const throw () [inline, inherited]

Get the address of the remote (peer) end of the socket.

bool getReuseAddress (  )  const throw (SocketException) [inherited]

Determine if the SO_REUSEADDR option is enabled or disabled.

Returns:
true if the option is enabled, false otherwise.
Exceptions:
SocketException If a socket error occurs.
size_t getSendBufSize (  )  const throw (SocketException) [inherited]

Get the size of the socket's internal send buffer.

Returns:
The size, in bytes.
Exceptions:
SocketException If the size could not be changed.
SocketHandle getSocketHandle (  )  const throw () [inline, protected, inherited]

Get the underlying socket handle for this socket.

bool getTCPDelay (  )  throw (SocketException) [inherited]

Determine if the Nagle algorithm is enabled or disabled.

Returns:
true if the Nagle algorithm is enabled (TCP_NODELAY off), false otherwise.
Exceptions:
SocketException If a socket error occurs.
NetProtocol getType (  )  const throw () [inline, inherited]

Get the socket type.

void init (  )  throw (SocketException) [virtual]

Initialize the socket.

This method creates the underlying socket object.

Exceptions:
SocketException If the socket could not be created.

Reimplemented from Socket.

bool isConnected (  )  const throw () [inline, inherited]

Determine if the socket is connected.

bool isInitialized (  )  const throw () [inline, inherited]

Determine if the socket has been initialized.

bool isListening (  )  const throw () [inline]

Determine if the socket is in a listening state.

void listen (  )  throw (SocketException)

Begin listening for connections on the socket.

Exceptions:
SocketException If a socket error occurs.
void setKeepAlive ( bool  enable  )  throw (SocketException) [inherited]

Enable or disable the SO_KEEPALIVE option on the socket.

Parameters:
enable true to enable the option false to disable it.
Exceptions:
SocketException If a socket error occurs.
void setLingerTime ( timespan_s_t  timeout  )  throw (SocketException) [inherited]

Set the linger time for the socket.

Parameters:
timeout The new linger time, in seconds; a negative value disables the linger feature.
Exceptions:
SocketException If the linger time could not be changed.
void setReceiveBufSize ( size_t  size  )  throw (SocketException) [inherited]

Set the size of the socket's internal receive buffer.

Parameters:
size The new size, in bytes.
Exceptions:
SocketException If the size could not be changed.
void setReuseAddress ( bool  enable  )  throw (SocketException) [inherited]

Enable or disable the SO_REUSEADDR option on the socket.

This method may only be called before the socket has been initialized. This option is off by default in newly created sockets.

Parameters:
enable true to enable the option false to disable it.
Exceptions:
SocketException If a socket error occurs, or if the call was made after the socket was initialized.
void setSendBufSize ( size_t  size  )  throw (SocketException) [inherited]

Set the size of the socket's internal send buffer.

Parameters:
size The new size, in bytes.
Exceptions:
SocketException If the size could not be changed.
void setTCPDelay ( bool  enable  )  throw (SocketException) [inherited]

Enable or disable the Nagle algorithm for send coalescing.

Parameters:
enable true to enable the Nagle algorithm (TCP_NODELAY off), false to disable it (TCP_NODELAY on).
Exceptions:
SocketException If a socket error occurs.
void setTimeout ( timespan_ms_t  timeout  )  throw (SocketException) [inherited]

Set the timeout for the socket.

The timeout affects both I/O and connections.

Parameters:
timeout The timeout, in milliseconds.
Exceptions:
SocketException If an error occurs.

Reimplemented in StreamSocket.

void shutdown (  )  throw () [virtual]

Shut down the socket.

Closes the connection and destroys the underlying socket object.

Reimplemented from Socket.

void waitForIO ( IOWaitMode  mode  )  throw (IOException) [protected, inherited]

Wait for the socket to become ready for reading or writing.

Parameters:
mode The wait mode; one of WaitWrite or WaitRead.
Exceptions:
IOException On timeout or other error.

Member Data Documentation

SocketAddress _laddr [protected, inherited]

The local address.

SocketAddress _raddr [protected, inherited]

The remote address.

SocketHandle _socket [protected, inherited]

A handle to the socket itself.

int _sotimeout [protected, inherited]

The timeout value.

NetProtocol _type [protected, inherited]

The socket type.


The documentation for this class was generated from the following files:
Generated on Sat Nov 26 16:49:09 2011 for libcommonc++ by  doxygen 1.6.3