The Micro C++ Library
micxx::DatagramSocket Class Reference

A DatagramSocket represents a User Datagram Protocol (UDP) Socket for sending and receiving single (short) datagram packets. More...

#include <micxx.hxx>

Inherits micxx::Socket.

Inherited by micxx::MulticastSocket.

List of all members.

Public Member Functions

 DatagramSocket ()
 Create a new DatagramSocket.
virtual void bind (unsigned short port=0)
 Bind the Socket to INADDR_ANY and the specified port.
virtual bool getRecvDstAddr () const
 The current value for socket option IP_PKTINFO (resp. IP_RECVDSTADDR for some systems like e.g. Solaris) of level IPPROTO_IP.
virtual long recv (Datagram &datagram)
 Try to receive a single datagram packet from the bound DatagramSocket.
virtual long sendto (const void *buffer, std::size_t size, const SocketAddress &socketAddress)
 Try to send a single datagram packet of the specified size to SocketAddress socketAddress.
virtual void setRecvDstAddr (bool value)
 Set the current value for socket option IP_PKTINFO (resp. IP_RECVDSTADDR for some systems like e.g. Solaris) of level IPPROTO_IP.

Protected Member Functions

virtual void createSocket (int addressFamily)
 Create the underlying socket descriptor for the specified addressFamily.

Detailed Description

A DatagramSocket represents a User Datagram Protocol (UDP) Socket for sending and receiving single (short) datagram packets.

UDP does not guarantee reliability or ordering of packets.

Example:

 micxx::DatagramSocket datagramSocket;
 micxx::InetAddress serverAddress(8000);
 datagramSocket.bind(serverAddress);
 micxx::Datagram datagram(MSG_SIZE);
 datagramSocket.recv(datagram);
 ...
Note:
Although DatagramSocket is derived from Mutex, itself does no locking at all. If you intend to use DatagramSocket concurrently from different threads, you could use the inherited Mutex to synchronize access to the instance (depending on the scenario in which it is used).
Author:
Norbert Klose
Date:
February, 2007

Constructor & Destructor Documentation

micxx::DatagramSocket::DatagramSocket ( )

Create a new DatagramSocket.

Same as Socket(SOCK_DGRAM).


Member Function Documentation

virtual void micxx::DatagramSocket::bind ( unsigned short  port = 0) [virtual]

Bind the Socket to INADDR_ANY and the specified port.

virtual void micxx::DatagramSocket::createSocket ( int  addressFamily) [protected, virtual]

Create the underlying socket descriptor for the specified addressFamily.

Parameters:
addressFamilyThe family of the stored SocketAddress.
See also:
SocketAddress::getFamily()

Reimplemented from micxx::Socket.

Reimplemented in micxx::MulticastSocket.

virtual bool micxx::DatagramSocket::getRecvDstAddr ( ) const [virtual]

The current value for socket option IP_PKTINFO (resp. IP_RECVDSTADDR for some systems like e.g. Solaris) of level IPPROTO_IP.

See also:
getsockopt(socket_t, IPPROTO_IP, IP_PKTINFO, void*, socklen_t*),
getsockopt(socket_t, IPPROTO_IP, IP_PKTINFO, void*, socklen_t*)
virtual long micxx::DatagramSocket::recv ( Datagram datagram) [virtual]

Try to receive a single datagram packet from the bound DatagramSocket.

Parameters:
datagramThe destination datagram where to store the received datagram packet.
Returns:
The number of bytes read into datagram.
See also:
Datagram::getSize(),
recvmsg(int, struct msghdr *, int)
Note:
The DatagramSocket must already be bound before recv(Datagram&) is called.
virtual long micxx::DatagramSocket::sendto ( const void *  buffer,
std::size_t  size,
const SocketAddress socketAddress 
) [virtual]

Try to send a single datagram packet of the specified size to SocketAddress socketAddress.

Note:
The DatagramSocket must already be bound before sendto(const void*, std::size_t, const SocketAddress&) is called.
virtual void micxx::DatagramSocket::setRecvDstAddr ( bool  value) [virtual]

Set the current value for socket option IP_PKTINFO (resp. IP_RECVDSTADDR for some systems like e.g. Solaris) of level IPPROTO_IP.

See also:
setsockopt(socket_t, IPPROTO_IP, IP_PKTINFO, const void*, socklen_t),
setsockopt(socket_t, IPPROTO_IP, IP_RECVDSTADDR, const void*, socklen_t)

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