nidas  v1.2-1520
Public Member Functions | Protected Attributes | List of all members
nidas::util::DatagramSocket Class Reference

A socket for sending or receiving datagrams, either unicast, broadcast or multicast. More...

#include <Socket.h>

Inheritance diagram for nidas::util::DatagramSocket:
Inheritance graph
[legend]

Public Member Functions

 DatagramSocket () throw (IOException)
 Create a DatagramSocket not bound to a port. More...
 
 DatagramSocket (int port) throw (IOException)
 Create a DatagramSocket bound to a port on all local interfaces (a local address of INADDR_ANY). More...
 
 throw (IOException)
 Creates a datagram socket and binds it to a port at the specified local address. More...
 
 throw (IOException)
 Creates a DatagramSocket and binds it to the specified local address. More...
 
virtual ~DatagramSocket () throw ()
 MulticastSocket is derived from DatagramSocket, so we provide a virtual destructor. More...
 
void close () throw (IOException)
 
void setTimeout (int val)
 Set the timeout for receive(), recv(), and recvfrom() methods. More...
 
void connect (const std::string &host, int port) throw (UnknownHostException,IOException)
 Datagrams are connectionless, so this doesn't establish a true connection, it just sets the default destination address for the send() calls. More...
 
void connect (const Inet4Address &addr, int port) throw (IOException)
 Datagrams are connectionless, so this doesn't establish a true connection, it just sets the default destination address for the send() calls. More...
 
void connect (const SocketAddress &addr) throw (IOException)
 Datagrams are connectionless, so this doesn't establish a true connection, it just sets the default destination address for the send() calls. More...
 
void bind (const Inet4Address &addr, int port) throw (IOException)
 Bind the DatagramSocket to the specified local address. More...
 
void bind (const SocketAddress &addr) throw (IOException)
 Bind the DatagramSocket to the specified local address. More...
 
int getFd () const
 
void receive (DatagramPacketBase &packet) throw (IOException)
 Read a packet from the DatagramSocket. More...
 
void receive (DatagramPacketBase &packet, Inet4PacketInfo &info, int flags=0) throw (IOException)
 Read a packet from the DatagramSocket. More...
 
void send (const DatagramPacketBase &packet, int flags=0) throw (IOException)
 
size_t recv (void *buf, size_t len, int flags=0) throw (IOException)
 
size_t recvfrom (void *buf, size_t len, int flags, SocketAddress &from) throw (IOException)
 
size_t send (const void *buf, size_t len, int flags=0) throw (IOException)
 
size_t send (const struct iovec *iov, int iovcnt, int flags=MSG_NOSIGNAL) throw (IOException)
 
size_t sendto (const void *buf, size_t len, int flags, const SocketAddress &to) throw (IOException)
 
size_t sendto (const struct iovec *iov, int iovcnt, int flags, const SocketAddress &to) throw (IOException)
 
const SocketAddressgetLocalSocketAddress () const throw ()
 Get local address of this socket. More...
 
int getLocalPort () const throw ()
 Get local port number of this socket. More...
 
void setNonBlocking (bool val) throw (IOException)
 Do fcntl system call to set O_NONBLOCK file descriptor flag on the socket. More...
 
bool isNonBlocking () const throw (IOException)
 
void setReceiveBufferSize (int size) throw (IOException)
 
int getReceiveBufferSize () throw (IOException)
 
void setSendBufferSize (int size) throw (IOException)
 
int getSendBufferSize () throw (IOException)
 
std::list< Inet4NetworkInterfacegetInterfaces () const throw (IOException)
 
void setBroadcastEnable (bool val) throw (IOException)
 
bool getBroadcastEnable () const throw (IOException)
 
void setPktInfo (bool val) throw (IOException)
 Control whether a IP_PKTINFO ancillary message is received with each datagram. More...
 

Protected Attributes

SocketImpl _impl
 

Detailed Description

A socket for sending or receiving datagrams, either unicast, broadcast or multicast.

This class provides the default public copy constructors and assignment operators. Objects of this class can be copied and assigned to without restriction. However, because of this, the destructor does not close the socket, so, in general, you should make sure that the socket is closed once at some point.

Typical usage: A receiver of datagrams on port 9000:

DatagramSocket sock(9000);
char buf[512];
Inet4SocketAddress from;
for (;;) {
sock.recvfrom(buf,sizeof(buf),0,from);
}
sock.close();

A unicast sender of datagrams on port 9000:

Inet4SocketAddress to(Inet4Address::getByName("128.117.80.99"),9000);
for (;;) {
sock.sendto("hello\n",6,0,to);
}
sock.close();

A multicast sender of datagrams on port 9000:

Inet4SocketAddress to(Inet4Address::getByName("239.0.0.1"),9000);
for (;;) {
sock.sendto("hello\n",6,0,to);
}
sock.close();

A broadcast sender of datagrams on port 9000 to the limited broadcast address of 255.255.255.255. This will fail with a "Network is unreachable" error if there are no external network interfaces UP with an assigned address. Use a broadcast address of 127.255.255.255 to "broadcast" on the loopback interface.

sock.setBroadcastEnable(true);
Inet4SocketAddress to(Inet4Address(INADDR_BROADCAST),9000);
for (;;) {
sock.sendto("hello\n",6,0,to);
}
sock.close();

Constructor & Destructor Documentation

DatagramSocket::DatagramSocket ( )
throw (IOException
)

Create a DatagramSocket not bound to a port.

DatagramSocket::DatagramSocket ( int  port)
throw (IOException
)

Create a DatagramSocket bound to a port on all local interfaces (a local address of INADDR_ANY).

Parameters
portPort number, 0<=port<=65535. If zero, the system will select an available port number. To find out which port number was selected, use getLocalPort().

References port.

virtual nidas::util::DatagramSocket::~DatagramSocket ( )
throw (
)
inlinevirtual

MulticastSocket is derived from DatagramSocket, so we provide a virtual destructor.

Currently there are no other virtual methods, may need some more thought.

Member Function Documentation

void nidas::util::DatagramSocket::bind ( const Inet4Address addr,
int  port 
)
throw (IOException
)
inline

Bind the DatagramSocket to the specified local address.

The address should correspond to the address of a local interface. Only packets sent to the given port and interface, by unicast, broadcast or multicast will be received.

Referenced by nidas::core::DatagramSocket::connect(), and nidas::core::DerivedDataReader::run().

void nidas::util::DatagramSocket::bind ( const SocketAddress addr)
throw (IOException
)
inline

Bind the DatagramSocket to the specified local address.

The address should correspond to the address of a local interface. Only packets sent to the given port and interface, by unicast, broadcast or multicast will be received.

void nidas::util::DatagramSocket::close ( )
throw (IOException
)
inline
void nidas::util::DatagramSocket::connect ( const std::string &  host,
int  port 
)
throw (UnknownHostException,
IOException
)
inline

Datagrams are connectionless, so this doesn't establish a true connection, it just sets the default destination address for the send() calls.

Referenced by nidas::core::DatagramSocket::connect(), and nidas::util::McSocketListener::run().

void nidas::util::DatagramSocket::connect ( const Inet4Address addr,
int  port 
)
throw (IOException
)
inline

Datagrams are connectionless, so this doesn't establish a true connection, it just sets the default destination address for the send() calls.

void nidas::util::DatagramSocket::connect ( const SocketAddress addr)
throw (IOException
)
inline

Datagrams are connectionless, so this doesn't establish a true connection, it just sets the default destination address for the send() calls.

bool nidas::util::DatagramSocket::getBroadcastEnable ( ) const
throw (IOException
)
inline
int nidas::util::DatagramSocket::getFd ( ) const
inline
std::list<Inet4NetworkInterface> nidas::util::DatagramSocket::getInterfaces ( ) const
throw (IOException
)
inline
int nidas::util::DatagramSocket::getLocalPort ( ) const
throw (
)
inline

Get local port number of this socket.

Referenced by nidas::core::StatusListener::run(), and nidas::util::McSocketMulticaster< SocketTT >::run().

const SocketAddress& nidas::util::DatagramSocket::getLocalSocketAddress ( ) const
throw (
)
inline
int nidas::util::DatagramSocket::getReceiveBufferSize ( )
throw (IOException
)
inline
int nidas::util::DatagramSocket::getSendBufferSize ( )
throw (IOException
)
inline
bool nidas::util::DatagramSocket::isNonBlocking ( ) const
throw (IOException
)
inline
void nidas::util::DatagramSocket::receive ( DatagramPacketBase packet)
throw (IOException
)
inline

Read a packet from the DatagramSocket.

On return, packet.getLength() will contain the number bytes received, and packet.getSocketAddress() will contain the address of the sender.

Referenced by PacketReader::loop(), nidas::core::DerivedDataReader::run(), nidas::util::McSocketListener::run(), and nidas::util::McSocketMulticaster< SocketTT >::run().

void nidas::util::DatagramSocket::receive ( DatagramPacketBase packet,
Inet4PacketInfo info,
int  flags = 0 
)
throw (IOException
)
inline

Read a packet from the DatagramSocket.

On return, packet.getLength() will contain the number bytes received, and packet.getSocketAddress() will contain the address of the sender. info will contain the information on the local and destination addresses of the packet, and the interface it was received on. If setPktInfo(true) has not be set on this socket, then it is set prior to receiving the packet, and then unset after receipt of the packet.

size_t nidas::util::DatagramSocket::recv ( void *  buf,
size_t  len,
int  flags = 0 
)
throw (IOException
)
inline
size_t nidas::util::DatagramSocket::recvfrom ( void *  buf,
size_t  len,
int  flags,
SocketAddress from 
)
throw (IOException
)
inline

References len.

Referenced by nidas::core::StatusListener::run().

void nidas::util::DatagramSocket::send ( const DatagramPacketBase packet,
int  flags = 0 
)
throw (IOException
)
inline
size_t nidas::util::DatagramSocket::send ( const void *  buf,
size_t  len,
int  flags = 0 
)
throw (IOException
)
inline
size_t nidas::util::DatagramSocket::send ( const struct iovec *  iov,
int  iovcnt,
int  flags = MSG_NOSIGNAL 
)
throw (IOException
)
inline
size_t nidas::util::DatagramSocket::sendto ( const void *  buf,
size_t  len,
int  flags,
const SocketAddress to 
)
throw (IOException
)
inline
size_t nidas::util::DatagramSocket::sendto ( const struct iovec *  iov,
int  iovcnt,
int  flags,
const SocketAddress to 
)
throw (IOException
)
inline
void nidas::util::DatagramSocket::setBroadcastEnable ( bool  val)
throw (IOException
)
inline
void nidas::util::DatagramSocket::setNonBlocking ( bool  val)
throw (IOException
)
inline

Do fcntl system call to set O_NONBLOCK file descriptor flag on the socket.

Parameters
valtrue=set O_NONBLOCK, false=unset O_NONBLOCK.

Referenced by nidas::core::DatagramSocket::connect(), nidas::core::McSocketUDP::connect(), nidas::core::McSocketUDP::connected(), and nidas::core::DatagramSocket::setNonBlocking().

void nidas::util::DatagramSocket::setPktInfo ( bool  val)
throw (IOException
)
inline

Control whether a IP_PKTINFO ancillary message is received with each datagram.

Only supported on DatagramSockets. The IP_PKTINFO message is converted to an Inet4PacketInfo object which is available via the getInet4PacketInfo() method.

Parameters
val,:if true enable the IP_PKTINFO message, if false, disable.
void nidas::util::DatagramSocket::setReceiveBufferSize ( int  size)
throw (IOException
)
inline
void nidas::util::DatagramSocket::setSendBufferSize ( int  size)
throw (IOException
)
inline
void nidas::util::DatagramSocket::setTimeout ( int  val)
inline

Set the timeout for receive(), recv(), and recvfrom() methods.

Parameters
valtimeout in milliseconds. 0=no timeout (infinite) The receive methods will return IOTimeoutException if an operation times out.
nidas::util::DatagramSocket::throw ( IOException  )

Creates a datagram socket and binds it to a port at the specified local address.

The address should correspond to the address of a local interface. Only packets sent the given port and interface, by unicast, broadcast or multicast will be received.

nidas::util::DatagramSocket::throw ( IOException  )

Creates a DatagramSocket and binds it to the specified local address.

The address should correspond to the address of a local interface. Only packets sent the given port and interface, by unicast, broadcast or multicast will be received.

Member Data Documentation

SocketImpl nidas::util::DatagramSocket::_impl
protected

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