DatagramSocket Class Reference

A User Datagram (UDP) socket. More...

#include <DatagramSocket.h++>

Inheritance diagram for DatagramSocket:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 DatagramSocket (uint16_t port=0) throw ()
 Construct a new DatagramSocket.
 ~DatagramSocket () throw ()
 Destructor.
void connect (const String &addr, uint16_t port) throw (IOException)
 Connect the datagram socket to a remote endpoint.
void connect (const SocketAddress &addr) throw (IOException)
 Connect the datagram socket to a remote endpoint.
size_t send (const byte_t *buffer, size_t buflen) throw (IOException)
 Send a datagram to the remote endpoint.
size_t send (ByteBuffer &buffer) throw (IOException)
 Send a datagram to the remote endpoint.
size_t send (const byte_t *buffer, size_t buflen, const SocketAddress &dest) throw (IOException)
 Send a datagram to a given address.
size_t send (ByteBuffer &buffer, const SocketAddress &dest) throw (IOException)
 Send a datagram to a given address.
size_t receive (byte_t *buffer, size_t buflen) throw (IOException)
 Receive a datagram from the remote endpoint, and write it to a buffer.
size_t receive (ByteBuffer &buffer) throw (IOException)
 Receive a datagram from the remote endpoint, and write it to a buffer.
size_t receive (byte_t *buffer, size_t buflen, SocketAddress &source) throw (IOException)
 Receive a datagram and write it to a buffer.
size_t receive (ByteBuffer &buffer, SocketAddress &source) throw (IOException)
 Receive a datagram and write it to a buffer.
void setBroadcast (bool flag) throw (SocketException)
 Enable or disable broadcast.
bool getBroadcast () const throw (SocketException)
 Determine if broadcast is enabled.
virtual void init () throw (SocketException)
 Initialize the socket.
virtual void shutdown () throw ()
 Shut down the socket.
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.

Static Public Attributes

static const size_t MAX_DATAGRAM_SIZE = 16384
 The maximum size of a datagram packet.

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 User Datagram (UDP) socket.

UDP sockets are connectionless and do not provide reliable delivery. Connecting a UDP socket does not establish an actual network connection, but rather specifies the default address to which datagrams are sent and the only address from which datagrams will be received.

Author:
Mark Lindner

Member Enumeration Documentation

enum IOWaitMode [protected, inherited]
Enumerator:
WaitWrite 
WaitRead 

Constructor & Destructor Documentation

DatagramSocket ( uint16_t  port = 0  )  throw ()

Construct a new DatagramSocket.

Parameters:
port The local port to bind to, or 0 to bind to an arbitrary port.
~DatagramSocket (  )  throw ()

Destructor.


Member Function Documentation

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

Connect the datagram socket to a remote endpoint.

Connecting a datagram socket to an address does not actually establish a network connection, but rather restricts the socket so that it may only exchange datagrams with that address.

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

Reimplemented from Socket.

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

Connect the datagram socket to a remote endpoint.

Connecting a datagram socket to an address does not actually establish a network connection, but rather restricts the socket so that it may only exchange datagrams with that address.

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 from Socket.

bool getBroadcast (  )  const throw (SocketException)

Determine if broadcast is enabled.

Returns:
true if broadcast is enabled, false otherwise.
Exceptions:
SocketException If an error occurs.
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, inherited]

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, inherited]

Determine if the socket is connected.

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

Determine if the socket has been initialized.

size_t receive ( ByteBuffer buffer,
SocketAddress source 
) throw (IOException)

Receive a datagram and write it to a buffer.

Parameters:
buffer The buffer to write the data to.
source The object in which to place the source address of the received datagram.
Returns:
The number of bytes received.
Exceptions:
IOException If an error occurs.
size_t receive ( byte_t buffer,
size_t  buflen,
SocketAddress source 
) throw (IOException)

Receive a datagram and write it to a buffer.

Parameters:
buffer The raw buffer to write the data to.
buflen The length of the buffer.
source The object in which to place the source address of the received datagram.
Returns:
The number of bytes received.
Exceptions:
IOException If an error occurs.
size_t receive ( ByteBuffer buffer  )  throw (IOException)

Receive a datagram from the remote endpoint, and write it to a buffer.

The socket must be connected before this method can be used.

Parameters:
buffer The buffer to write the data to.
Returns:
The number of bytes received.
Exceptions:
IOException If an error occurs.
size_t receive ( byte_t buffer,
size_t  buflen 
) throw (IOException)

Receive a datagram from the remote endpoint, and write it to a buffer.

The socket must be connected before this method can be used.

Parameters:
buffer The raw buffer to write the data to.
buflen The length of the buffer.
Returns:
The number of bytes received.
Exceptions:
IOException If an error occurs.
size_t send ( ByteBuffer buffer,
const SocketAddress dest 
) throw (IOException)

Send a datagram to a given address.

Parameters:
buffer The buffer containing the data to send.
dest The destination address.
Returns:
The number of bytes sent.
Exceptions:
IOException If an error occurs.
size_t send ( const byte_t buffer,
size_t  buflen,
const SocketAddress dest 
) throw (IOException)

Send a datagram to a given address.

Parameters:
buffer The raw buffer containing the data to send.
buflen The length of the buffer.
dest The destination address.
Returns:
The number of bytes sent.
Exceptions:
IOException If an error occurs.
size_t send ( ByteBuffer buffer  )  throw (IOException)

Send a datagram to the remote endpoint.

The socket must be connected before this method can be used.

Parameters:
buffer The buffer containing the data to send.
Returns:
The number of bytes sent.
Exceptions:
IOException If an error occurs.
size_t send ( const byte_t buffer,
size_t  buflen 
) throw (IOException)

Send a datagram to the remote endpoint.

The socket must be connected before this method can be used.

Parameters:
buffer The raw buffer containing the data to send.
buflen The length of the buffer.
Returns:
The number of bytes sent.
Exceptions:
IOException If an error occurs.
void setBroadcast ( bool  flag  )  throw (SocketException)

Enable or disable broadcast.

When enabled, the socket will receive packets sent to a broadcast address and is allowed to send packets to a broadcast address.

Parameters:
flag A flag indicating whether the feature should be enabled or disabled.
Exceptions:
SocketException If an 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, inherited]

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, 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.

const size_t MAX_DATAGRAM_SIZE = 16384 [static]

The maximum size of a datagram packet.


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