libcommonc++  0.7
Socket Class Reference

A base class for network sockets. More...

#include <Socket.h++>

Inheritance diagram for Socket:
Collaboration diagram for Socket:

Public Member Functions

virtual ~Socket ()
 Destructor. More...
 
virtual void init ()
 Initialize the socket. More...
 
virtual void connect (const String &addr, uint16_t port)
 Connect the socket to a remote endpoint. More...
 
virtual void connect (const SocketAddress &addr)
 Connect the socket to a remote endpoint. More...
 
virtual void shutdown ()
 Shut down the socket. More...
 
void setTimeout (timespan_ms_t timeout)
 Set the timeout for the socket. More...
 
void setReceiveBufSize (size_t size)
 Set the size of the socket's internal receive buffer. More...
 
size_t getReceiveBufSize () const
 Get the size of the socket's internal receive buffer. More...
 
void setSendBufSize (size_t size)
 Set the size of the socket's internal send buffer. More...
 
size_t getSendBufSize () const
 Get the size of the socket's internal send buffer. More...
 
void setLingerTime (timespan_s_t timeout)
 Set the linger time for the socket. More...
 
timespan_s_t getLingerTime () const
 Get the linger time for the socket. More...
 
void setReuseAddress (bool enable)
 Enable or disable the SO_REUSEADDR option on the socket. More...
 
bool getReuseAddress () const
 Determine if the SO_REUSEADDR option is enabled or disabled. More...
 
void setKeepAlive (bool enable)
 Enable or disable the SO_KEEPALIVE option on the socket. More...
 
bool getKeepAlive ()
 Determine if the SO_KEEPALIVE option is enabled or disabled. More...
 
void setTCPDelay (bool enable)
 Enable or disable the Nagle algorithm for send coalescing. More...
 
bool getTCPDelay ()
 Determine if the Nagle algorithm is enabled or disabled. More...
 
bool isInitialized () const
 Determine if the socket has been initialized. More...
 
bool isConnected () const
 Determine if the socket is connected. More...
 
NetProtocol getType () const
 Get the socket type. More...
 
const SocketAddressgetLocalAddress () const
 Get the address of the local end of the socket. More...
 
const SocketAddressgetRemoteAddress () const
 Get the address of the remote (peer) end of the socket. More...
 

Protected Types

enum  IOWaitMode { WaitWrite, WaitRead }
 

Protected Member Functions

 Socket (NetProtocol type=ProtoTCP)
 Construct a new Socket of the specified type. More...
 
SocketHandle getSocketHandle () const
 Get the underlying socket handle for this socket. More...
 
void waitForIO (IOWaitMode mode)
 Wait for the socket to become ready for reading or writing. More...
 

Protected Attributes

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

Friends

class ServerSocket
 
class SocketSelector
 

Detailed Description

A base class for network sockets.

Author
Mark Lindner

Member Enumeration Documentation

◆ IOWaitMode

enum IOWaitMode
protected
Enumerator
WaitWrite 
WaitRead 

Constructor & Destructor Documentation

◆ Socket()

Socket ( NetProtocol  type = ProtoTCP)
protected

Construct a new Socket of the specified type.

Parameters
typeThe socket type.

◆ ~Socket()

~Socket ( )
virtual

Destructor.

Closes and destroys the socket.

Member Function Documentation

◆ connect() [1/2]

void connect ( const String addr,
uint16_t  port 
)
virtual

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
addrThe address, which may either be a dot-separated IP address or a DNS name.
portThe port number.
Exceptions
IOExceptionIf an error occurs.

Reimplemented in DatagramSocket, and StreamSocket.

◆ connect() [2/2]

void connect ( const SocketAddress addr)
virtual

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
addrThe address.
Exceptions
IOExceptionIf an error occurs.

Reimplemented in DatagramSocket, and StreamSocket.

◆ getKeepAlive()

bool getKeepAlive ( )

Determine if the SO_KEEPALIVE option is enabled or disabled.

Returns
true if the option is enabled, false otherwise.
Exceptions
SocketExceptionIf a socket error occurs.

◆ getLingerTime()

timespan_s_t getLingerTime ( ) const

Get the linger time for the socket.

Returns
The linger time, in seconds, or -1 if the feature is disabled.
Exceptions
SocketExceptionIf the linger time could not be retrieved.

◆ getLocalAddress()

const SocketAddress& getLocalAddress ( ) const
inline

Get the address of the local end of the socket.

◆ getReceiveBufSize()

size_t getReceiveBufSize ( ) const

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

Returns
The size, in bytes.
Exceptions
SocketExceptionIf the size could not be changed.

◆ getRemoteAddress()

const SocketAddress& getRemoteAddress ( ) const
inline

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

◆ getReuseAddress()

bool getReuseAddress ( ) const

Determine if the SO_REUSEADDR option is enabled or disabled.

Returns
true if the option is enabled, false otherwise.
Exceptions
SocketExceptionIf a socket error occurs.

◆ getSendBufSize()

size_t getSendBufSize ( ) const

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

Returns
The size, in bytes.
Exceptions
SocketExceptionIf the size could not be changed.

◆ getSocketHandle()

SocketHandle getSocketHandle ( ) const
inlineprotected

Get the underlying socket handle for this socket.

◆ getTCPDelay()

bool getTCPDelay ( )

Determine if the Nagle algorithm is enabled or disabled.

Returns
true if the Nagle algorithm is enabled (TCP_NODELAY off), false otherwise.
Exceptions
SocketExceptionIf a socket error occurs.

◆ getType()

NetProtocol getType ( ) const
inline

Get the socket type.

◆ init()

void init ( )
virtual

Initialize the socket.

This method creates the underlying socket object.

Exceptions
SocketExceptionIf the socket could not be created.

Reimplemented in ServerSocket.

◆ isConnected()

bool isConnected ( ) const
inline

Determine if the socket is connected.

◆ isInitialized()

bool isInitialized ( ) const
inline

Determine if the socket has been initialized.

◆ setKeepAlive()

void setKeepAlive ( bool  enable)

Enable or disable the SO_KEEPALIVE option on the socket.

Parameters
enabletrue to enable the option false to disable it.
Exceptions
SocketExceptionIf a socket error occurs.

◆ setLingerTime()

void setLingerTime ( timespan_s_t  timeout)

Set the linger time for the socket.

Parameters
timeoutThe new linger time, in seconds; a negative value disables the linger feature.
Exceptions
SocketExceptionIf the linger time could not be changed.

◆ setReceiveBufSize()

void setReceiveBufSize ( size_t  size)

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

Parameters
sizeThe new size, in bytes.
Exceptions
SocketExceptionIf the size could not be changed.

◆ setReuseAddress()

void setReuseAddress ( bool  enable)

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
enabletrue to enable the option false to disable it.
Exceptions
SocketExceptionIf a socket error occurs, or if the call was made after the socket was initialized.

◆ setSendBufSize()

void setSendBufSize ( size_t  size)

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

Parameters
sizeThe new size, in bytes.
Exceptions
SocketExceptionIf the size could not be changed.

◆ setTCPDelay()

void setTCPDelay ( bool  enable)

Enable or disable the Nagle algorithm for send coalescing.

Parameters
enabletrue to enable the Nagle algorithm (TCP_NODELAY off), false to disable it (TCP_NODELAY on).
Exceptions
SocketExceptionIf a socket error occurs.

◆ setTimeout()

void setTimeout ( timespan_ms_t  timeout)

Set the timeout for the socket.

The timeout affects both I/O and connections.

Parameters
timeoutThe timeout, in milliseconds.
Exceptions
SocketExceptionIf an error occurs.

◆ shutdown()

void shutdown ( )
virtual

Shut down the socket.

Closes the connection and destroys the underlying socket object.

Reimplemented in StreamSocket, and ServerSocket.

◆ waitForIO()

void waitForIO ( IOWaitMode  mode)
protected

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

Parameters
modeThe wait mode; one of WaitWrite or WaitRead.
Exceptions
IOExceptionOn timeout or other error.

Friends And Related Function Documentation

◆ ServerSocket

friend class ServerSocket
friend

◆ SocketSelector

friend class SocketSelector
friend

Member Data Documentation

◆ _laddr

SocketAddress _laddr
protected

The local address.

◆ _raddr

SocketAddress _raddr
protected

The remote address.

◆ _socket

SocketHandle _socket
protected

A handle to the socket itself.

◆ _sotimeout

int _sotimeout
protected

The timeout value.

◆ _type

NetProtocol _type
protected

The socket type.


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