Socket Class Reference

A base class for network sockets. More...

#include <Socket.h++>

Inheritance diagram for Socket:

Inheritance graph
[legend]
Collaboration diagram for Socket:

Collaboration graph
[legend]

List of all members.

Public Member Functions

virtual ~Socket () throw ()
 Destructor.
virtual void init () throw (SocketException)
 Initialize 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.
virtual void shutdown () throw ()
 Shut down the socket.
void setTimeout (int msec) 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 (int msec) throw (SocketException)
 Set the linger time for the socket.
int 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

 Socket (NetProtocol type=ProtoTCP) throw ()
 Construct a new Socket of the specified type.
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.
bool _connected
bool _reuseAddr

Friends

class ServerSocket
class SocketMuxer


Detailed Description

A base class for network sockets.

Author:
Mark Lindner

Member Enumeration Documentation

enum IOWaitMode [protected]

Enumerator:
WaitWrite 
WaitRead 


Constructor & Destructor Documentation

Socket ( NetProtocol  type = ProtoTCP  )  throw () [protected]

Construct a new Socket of the specified type.

Parameters:
type The socket type.

~Socket (  )  throw () [virtual]

Destructor.

Closes and destroys the socket.


Member Function Documentation

void connect ( const SocketAddress addr  )  throw (IOException) [virtual]

Connect the socket to a remote endpoint.

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]

Connect the socket to a remote endpoint.

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)

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.

int getLingerTime (  )  const throw (SocketException)

Get the linger time for the socket.

Returns:
The linger time, in milliseconds, or -1 if the feature is disabled.
Exceptions:
SocketException If the linger time could not be retrieved.

const SocketAddress& getLocalAddress (  )  const throw () [inline]

Get the address of the local end of the socket.

size_t getReceiveBufSize (  )  const throw (SocketException)

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]

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

bool getReuseAddress (  )  const throw (SocketException)

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)

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]

Get the underlying socket handle for this socket.

bool getTCPDelay (  )  throw (SocketException)

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]

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 in ServerSocket.

bool isConnected (  )  const throw () [inline]

Determine if the socket is connected.

bool isInitialized (  )  const throw () [inline]

Determine if the socket has been initialized.

void setKeepAlive ( bool  enable  )  throw (SocketException)

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 ( int  msec  )  throw (SocketException)

Set the linger time for the socket.

Parameters:
msec The new linger time, in milliseconds; a negative value disables the linger feature.
Exceptions:
SocketException If the linger time could not be changed.

void setReceiveBufSize ( size_t  size  )  throw (SocketException)

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)

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)

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)

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 ( int  msec  )  throw (SocketException)

Set the timeout for the socket.

The timeout affects both I/O and connections.

Parameters:
msec 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 in ServerSocket, and StreamSocket.

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

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.


Friends And Related Function Documentation

friend class ServerSocket [friend]

Reimplemented in StreamSocket.

friend class SocketMuxer [friend]


Member Data Documentation

bool _connected [protected]

SocketAddress _laddr [protected]

The local address.

SocketAddress _raddr [protected]

The remote address.

bool _reuseAddr [protected]

SocketHandle _socket [protected]

A handle to the socket itself.

int _sotimeout [protected]

The timeout value.

NetProtocol _type [protected]

The socket type.


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

Generated on Sat Apr 17 23:03:10 2010 for libcommonc++ by  doxygen 1.5.9