libcommonc++
0.7
|
A User Datagram (UDP) socket. More...
#include <DatagramSocket.h++>
Public Member Functions | |
DatagramSocket (uint16_t port=0) | |
Construct a new DatagramSocket. More... | |
~DatagramSocket () | |
Destructor. More... | |
void | connect (const String &addr, uint16_t port) |
Connect the datagram socket to a remote endpoint. More... | |
void | connect (const SocketAddress &addr) |
Connect the datagram socket to a remote endpoint. More... | |
size_t | send (const byte_t *buffer, size_t buflen) |
Send a datagram to the remote endpoint. More... | |
size_t | send (ByteBuffer &buffer) |
Send a datagram to the remote endpoint. More... | |
size_t | send (const byte_t *buffer, size_t buflen, const SocketAddress &dest) |
Send a datagram to a given address. More... | |
size_t | send (ByteBuffer &buffer, const SocketAddress &dest) |
Send a datagram to a given address. More... | |
size_t | receive (byte_t *buffer, size_t buflen) |
Receive a datagram from the remote endpoint, and write it to a buffer. More... | |
size_t | receive (ByteBuffer &buffer) |
Receive a datagram from the remote endpoint, and write it to a buffer. More... | |
size_t | receive (byte_t *buffer, size_t buflen, SocketAddress &source) |
Receive a datagram and write it to a buffer. More... | |
size_t | receive (ByteBuffer &buffer, SocketAddress &source) |
Receive a datagram and write it to a buffer. More... | |
void | setBroadcast (bool flag) |
Enable or disable broadcast. More... | |
bool | getBroadcast () const |
Determine if broadcast is enabled. More... | |
virtual void | init () |
Initialize the socket. 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 SocketAddress & | getLocalAddress () const |
Get the address of the local end of the socket. More... | |
const SocketAddress & | getRemoteAddress () const |
Get the address of the remote (peer) end of the socket. More... | |
Static Public Attributes | |
static const size_t | MAX_DATAGRAM_SIZE = 16384 |
The maximum size of a datagram packet. More... | |
Protected Types | |
enum | IOWaitMode { WaitWrite, WaitRead } |
Protected Member Functions | |
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... | |
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.
|
protectedinherited |
DatagramSocket | ( | uint16_t | port = 0 | ) |
Construct a new DatagramSocket.
port | The local port to bind to, or 0 to bind to an arbitrary port. |
~DatagramSocket | ( | ) |
Destructor.
|
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.
addr | The address, which may either be a dot-separated IP address or a DNS name. |
port | The port number. |
IOException | If an error occurs. |
Reimplemented from Socket.
|
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.
addr | The address. |
IOException | If an error occurs. |
Reimplemented from Socket.
bool getBroadcast | ( | ) | const |
Determine if broadcast is enabled.
SocketException | If an error occurs. |
|
inherited |
Determine if the SO_KEEPALIVE option is enabled or disabled.
SocketException | If a socket error occurs. |
|
inherited |
Get the linger time for the socket.
SocketException | If the linger time could not be retrieved. |
|
inlineinherited |
Get the address of the local end of the socket.
|
inherited |
Get the size of the socket's internal receive buffer.
SocketException | If the size could not be changed. |
|
inlineinherited |
Get the address of the remote (peer) end of the socket.
|
inherited |
Determine if the SO_REUSEADDR option is enabled or disabled.
SocketException | If a socket error occurs. |
|
inherited |
Get the size of the socket's internal send buffer.
SocketException | If the size could not be changed. |
|
inlineprotectedinherited |
Get the underlying socket handle for this socket.
|
inherited |
Determine if the Nagle algorithm is enabled or disabled.
SocketException | If a socket error occurs. |
|
inlineinherited |
Get the socket type.
|
virtualinherited |
Initialize the socket.
This method creates the underlying socket object.
SocketException | If the socket could not be created. |
Reimplemented in ServerSocket.
|
inlineinherited |
Determine if the socket is connected.
|
inlineinherited |
Determine if the socket has been initialized.
size_t receive | ( | byte_t * | buffer, |
size_t | buflen | ||
) |
Receive a datagram from the remote endpoint, and write it to a buffer.
The socket must be connected before this method can be used.
buffer | The raw buffer to write the data to. |
buflen | The length of the buffer. |
IOException | If an error occurs. |
size_t receive | ( | ByteBuffer & | buffer | ) |
Receive a datagram from the remote endpoint, and write it to a buffer.
The socket must be connected before this method can be used.
buffer | The buffer to write the data to. |
IOException | If an error occurs. |
size_t receive | ( | byte_t * | buffer, |
size_t | buflen, | ||
SocketAddress & | source | ||
) |
Receive a datagram and write it to a buffer.
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. |
IOException | If an error occurs. |
size_t receive | ( | ByteBuffer & | buffer, |
SocketAddress & | source | ||
) |
Receive a datagram and write it to a buffer.
buffer | The buffer to write the data to. |
source | The object in which to place the source address of the received datagram. |
IOException | If an error occurs. |
size_t send | ( | const byte_t * | buffer, |
size_t | buflen | ||
) |
Send a datagram to the remote endpoint.
The socket must be connected before this method can be used.
buffer | The raw buffer containing the data to send. |
buflen | The length of the buffer. |
IOException | If an error occurs. |
size_t send | ( | ByteBuffer & | buffer | ) |
Send a datagram to the remote endpoint.
The socket must be connected before this method can be used.
buffer | The buffer containing the data to send. |
IOException | If an error occurs. |
size_t send | ( | const byte_t * | buffer, |
size_t | buflen, | ||
const SocketAddress & | dest | ||
) |
Send a datagram to a given address.
buffer | The raw buffer containing the data to send. |
buflen | The length of the buffer. |
dest | The destination address. |
IOException | If an error occurs. |
size_t send | ( | ByteBuffer & | buffer, |
const SocketAddress & | dest | ||
) |
Send a datagram to a given address.
buffer | The buffer containing the data to send. |
dest | The destination address. |
IOException | If an error occurs. |
void setBroadcast | ( | bool | flag | ) |
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.
flag | A flag indicating whether the feature should be enabled or disabled. |
SocketException | If an error occurs. |
|
inherited |
Enable or disable the SO_KEEPALIVE option on the socket.
enable | true to enable the option false to disable it. |
SocketException | If a socket error occurs. |
|
inherited |
Set the linger time for the socket.
timeout | The new linger time, in seconds; a negative value disables the linger feature. |
SocketException | If the linger time could not be changed. |
|
inherited |
Set the size of the socket's internal receive buffer.
size | The new size, in bytes. |
SocketException | If the size could not be changed. |
|
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.
enable | true to enable the option false to disable it. |
SocketException | If a socket error occurs, or if the call was made after the socket was initialized. |
|
inherited |
Set the size of the socket's internal send buffer.
size | The new size, in bytes. |
SocketException | If the size could not be changed. |
|
inherited |
Enable or disable the Nagle algorithm for send coalescing.
enable | true to enable the Nagle algorithm (TCP_NODELAY off), false to disable it (TCP_NODELAY on). |
SocketException | If a socket error occurs. |
|
inherited |
Set the timeout for the socket.
The timeout affects both I/O and connections.
timeout | The timeout, in milliseconds. |
SocketException | If an error occurs. |
|
virtualinherited |
Shut down the socket.
Closes the connection and destroys the underlying socket object.
Reimplemented in StreamSocket, and ServerSocket.
|
protectedinherited |
Wait for the socket to become ready for reading or writing.
mode | The wait mode; one of WaitWrite or WaitRead. |
IOException | On timeout or other error. |
|
protectedinherited |
The local address.
|
protectedinherited |
The remote address.
|
protectedinherited |
A handle to the socket itself.
|
protectedinherited |
The timeout value.
|
protectedinherited |
The socket type.
|
static |
The maximum size of a datagram packet.