nidas v1.2.3
Public Member Functions | Protected Attributes | List of all members
nidas::util::MulticastSocket Class Reference

A datagram socket to be used for multicasts. More...

#include <Socket.h>

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

Public Member Functions

 MulticastSocket ()
 Create an unbound multicast socket.
 
 MulticastSocket (int port)
 Create multicast socket, bind it to a local port.
 
 MulticastSocket (const Inet4SocketAddress &addr)
 Creates a datagram socket and binds it to the specified local address.
 
void joinGroup (Inet4Address groupAddr)
 Join a multicast group on all interfaces.
 
void joinGroup (Inet4Address groupAddr, const Inet4NetworkInterface &iface)
 Join a multicast group on a given interface.
 
void leaveGroup (Inet4Address groupAddr)
 Leave a multicast group on all interfaces.
 
void leaveGroup (Inet4Address groupAddr, const Inet4NetworkInterface &iface)
 Leave a multicast group on a given interface.
 
void setTimeToLive (int val)
 
int getTimeToLive () const
 
void setMulticastLoop (bool val)
 Whether to set the IP_MULTICAST_LOOP socket option.
 
bool getMulticastLoop () const
 
void setInterface (Inet4Address maddr, const Inet4NetworkInterface &iface)
 Set the interface for a given multicast address.
 
void setInterface (Inet4Address iaddr)
 Set the interface for a given multicast address.
 
Inet4NetworkInterface getInterface () const
 
Inet4NetworkInterface findInterface (const Inet4Address &iaddr) const
 
std::list< Inet4NetworkInterfacegetInterfaces () const
 Return the IP addresses of all my network interfaces.
 
void close ()
 
void setTimeout (int val)
 Set the timeout for receive(), recv(), and recvfrom() methods.
 
void connect (const std::string &host, int port)
 Datagrams are connectionless, so this doesn't establish a true connection, it just sets the default destination address for the send() calls.
 
void connect (const Inet4Address &addr, int port)
 Datagrams are connectionless, so this doesn't establish a true connection, it just sets the default destination address for the send() calls.
 
void connect (const SocketAddress &addr)
 Datagrams are connectionless, so this doesn't establish a true connection, it just sets the default destination address for the send() calls.
 
void bind (const Inet4Address &addr, int port)
 Bind the DatagramSocket to the specified local address.
 
void bind (const SocketAddress &addr)
 Bind the DatagramSocket to the specified local address.
 
int getFd () const
 
void receive (DatagramPacketBase &packet)
 Read a packet from the DatagramSocket.
 
void receive (DatagramPacketBase &packet, Inet4PacketInfo &info, int flags=0)
 Read a packet from the DatagramSocket.
 
void send (const DatagramPacketBase &packet, int flags=0)
 
size_t send (const void *buf, size_t len, int flags=0)
 
size_t send (const struct iovec *iov, int iovcnt, int flags=MSG_NOSIGNAL)
 
size_t recv (void *buf, size_t len, int flags=0)
 
size_t recvfrom (void *buf, size_t len, int flags, SocketAddress &from)
 
size_t sendto (const void *buf, size_t len, int flags, const SocketAddress &to)
 
size_t sendto (const struct iovec *iov, int iovcnt, int flags, const SocketAddress &to)
 
const SocketAddressgetLocalSocketAddress () const
 Get local address of this socket.
 
int getLocalPort () const
 Get local port number of this socket.
 
void setNonBlocking (bool val)
 Do fcntl system call to set O_NONBLOCK file descriptor flag on the socket.
 
bool isNonBlocking () const
 
void setReceiveBufferSize (int size)
 
int getReceiveBufferSize ()
 
void setSendBufferSize (int size)
 
int getSendBufferSize ()
 
void setBroadcastEnable (bool val)
 
bool getBroadcastEnable () const
 
void setPktInfo (bool val)
 Control whether a IP_PKTINFO ancillary message is received with each datagram.
 

Protected Attributes

SocketImpl _impl
 

Detailed Description

A datagram socket to be used for multicasts.

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.

Usage example:

// host A listens for multicasts to address 239.0.0.1, port 9000:
MulticastSocket msock(9000);
// join the multicast group in whatever interface the system chooses,
// typically the first ethernet interface.
msock.joinGroup(Inet4Address::getByName("239.0.0.1"));
char buf[512];
for (;;) {
msock.recvfrom(buf,sizeof(buf),0,from);
...
}
// how to join on all multicast and loopback interfaces
// note that multicast works over the loopback interface
list<Inet4NetworkInterface> ifaces = msock.getInterfaces();
for (list<Inet4NetworkInterface>::const_iterator ii = ifaces.begin();
ii != ifaces.end(); ++ii)
{
if ((ii->getFlags() & (IFF_LOOPBACK | IFF_MULTICAST)) && (ii->getFlags() & IFF_UP))
msock.joinGroup(Inet4Address::getByName("239.0.0.1"),*ii);
}
// host B multicasts to address 239.0.0.1, port 9000:
Inet4Address maddr = Inet4Address::getByName("239.0.0.1");
Inet4SocketAddress msaddr = Inet4SocketAddress(maddr,9000);
for (;;) {
msock.sendto("hello\n",6,0,msaddr);
...
}
// host C also does similar multicasts, but uses MulticastSocket
// methods to send packets out a specific interface, and
// to set the time to live (TTL) parameter.
Inet4Address maddr = Inet4Address::getByName("239.0.0.1");
list<Inet4NetworkInterfaces> ifaces = msock.getInterfaces();
list<Inet4NetworkInterfaces>::const_iterator ii;
for (ii = ifaces.begin(); ii != ifaces.end(); ++ii) {
Inet4NetworkInterface ni = *ii;
if (ni.getName() == "eth1")
msock.setInterface(maddr,ni);
}
msock.setTimeToLive(2); // go through one router
Inet4SocketAddress msaddr = Inet4SocketAddress(maddr,9000);
for (;;) {
msock.sendto("hello\n",6,0,msaddr);
...
}
DatagramSocket()
Create a DatagramSocket not bound to a port.
Definition Socket.cc:1554
static Inet4Address getByName(const std::string &hostname)
Return an address of a name, the first one found by getAllByName.
Definition Inet4Address.cc:175
A IP version 4 socket address, containing a host address, and a port number.
Definition Inet4SocketAddress.h:41
A datagram socket to be used for multicasts.
Definition Socket.h:1582
MulticastSocket()
Create an unbound multicast socket.
Definition Socket.h:1596

Note: if the above examples are not working for you, check your firewall settings. They might be blocking multicasts.

Constructor & Destructor Documentation

◆ MulticastSocket() [1/3]

nidas::util::MulticastSocket::MulticastSocket ( )
inline

Create an unbound multicast socket.

You must bind it to a port if you want to receive packets. If you are sending packets on this MulticastSocket, and do not use getInterfaces() and setInterface() to choose a specific interface, the system sends packets out on the first non-loopback interface that it finds. If a firewall is blocking multicast packets on that interface the packets won't be sent.

Exceptions
IOException

◆ MulticastSocket() [2/3]

nidas::util::MulticastSocket::MulticastSocket ( int port)
inline

Create multicast socket, bind it to a local port.

Exceptions
IOException

◆ MulticastSocket() [3/3]

nidas::util::MulticastSocket::MulticastSocket ( const Inet4SocketAddress & addr)
inline

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

Exceptions
IOException

Member Function Documentation

◆ bind() [1/2]

void nidas::util::DatagramSocket::bind ( const Inet4Address & addr,
int port )
inlineinherited

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.

Exceptions
IOException

References nidas::util::DatagramSocket::_impl, nidas::util::SocketImpl::bind(), and port.

Referenced by nidas::core::DatagramSocket::connect(), and nidas::core::UDPSocketIODevice::open().

◆ bind() [2/2]

void nidas::util::DatagramSocket::bind ( const SocketAddress & addr)
inlineinherited

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.

Exceptions
IOException

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::bind().

◆ close()

void nidas::util::DatagramSocket::close ( )
inlineinherited

◆ connect() [1/3]

void nidas::util::DatagramSocket::connect ( const Inet4Address & addr,
int port )
inlineinherited

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

Exceptions
IOException

References nidas::util::DatagramSocket::_impl, nidas::util::SocketImpl::connect(), and port.

◆ connect() [2/3]

void nidas::util::DatagramSocket::connect ( const SocketAddress & addr)
inlineinherited

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

Exceptions
IOException

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::connect().

◆ connect() [3/3]

void nidas::util::DatagramSocket::connect ( const std::string & host,
int port )
inlineinherited

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

Exceptions
UnknownHostException
IOException

References nidas::util::DatagramSocket::_impl, nidas::util::SocketImpl::connect(), and port.

Referenced by nidas::core::DatagramSocket::connect().

◆ findInterface()

Inet4NetworkInterface nidas::util::MulticastSocket::findInterface ( const Inet4Address & iaddr) const
inline

◆ getBroadcastEnable()

bool nidas::util::DatagramSocket::getBroadcastEnable ( ) const
inlineinherited

◆ getFd()

int nidas::util::DatagramSocket::getFd ( ) const
inlineinherited

◆ getInterface()

Inet4NetworkInterface nidas::util::MulticastSocket::getInterface ( ) const
inline

◆ getInterfaces()

std::list< Inet4NetworkInterface > nidas::util::MulticastSocket::getInterfaces ( ) const
inline

Return the IP addresses of all my network interfaces.

Exceptions
IOException

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::getInterfaces().

◆ getLocalPort()

int nidas::util::DatagramSocket::getLocalPort ( ) const
inlineinherited

Get local port number of this socket.

Exceptions

)

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::getLocalPort().

◆ getLocalSocketAddress()

const SocketAddress & nidas::util::DatagramSocket::getLocalSocketAddress ( ) const
inlineinherited

Get local address of this socket.

Exceptions

)

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::getLocalSocketAddress().

Referenced by nidas::util::McSocketListener::run().

◆ getMulticastLoop()

bool nidas::util::MulticastSocket::getMulticastLoop ( ) const
inline

◆ getReceiveBufferSize()

int nidas::util::DatagramSocket::getReceiveBufferSize ( )
inlineinherited

◆ getSendBufferSize()

int nidas::util::DatagramSocket::getSendBufferSize ( )
inlineinherited

◆ getTimeToLive()

int nidas::util::MulticastSocket::getTimeToLive ( ) const
inline

◆ isNonBlocking()

bool nidas::util::DatagramSocket::isNonBlocking ( ) const
inlineinherited

◆ joinGroup() [1/2]

void nidas::util::MulticastSocket::joinGroup ( Inet4Address groupAddr)
inline

Join a multicast group on all interfaces.

Exceptions
IOException

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::joinGroup().

◆ joinGroup() [2/2]

void nidas::util::MulticastSocket::joinGroup ( Inet4Address groupAddr,
const Inet4NetworkInterface & iface )
inline

Join a multicast group on a given interface.

Exceptions
IOException

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::joinGroup().

◆ leaveGroup() [1/2]

void nidas::util::MulticastSocket::leaveGroup ( Inet4Address groupAddr)
inline

Leave a multicast group on all interfaces.

Exceptions
IOException

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::leaveGroup().

◆ leaveGroup() [2/2]

void nidas::util::MulticastSocket::leaveGroup ( Inet4Address groupAddr,
const Inet4NetworkInterface & iface )
inline

Leave a multicast group on a given interface.

Exceptions
IOException

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::leaveGroup().

◆ receive() [1/2]

void nidas::util::DatagramSocket::receive ( DatagramPacketBase & packet)
inlineinherited

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.

Exceptions
IOException

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::receive().

Referenced by nidas::util::McSocketListener::run().

◆ receive() [2/2]

void nidas::util::DatagramSocket::receive ( DatagramPacketBase & packet,
Inet4PacketInfo & info,
int flags = 0 )
inlineinherited

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.

Exceptions
IOException

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::receive().

◆ recv()

size_t nidas::util::DatagramSocket::recv ( void * buf,
size_t len,
int flags = 0 )
inlineinherited

◆ recvfrom()

size_t nidas::util::DatagramSocket::recvfrom ( void * buf,
size_t len,
int flags,
SocketAddress & from )
inlineinherited

◆ send() [1/3]

void nidas::util::DatagramSocket::send ( const DatagramPacketBase & packet,
int flags = 0 )
inlineinherited

◆ send() [2/3]

size_t nidas::util::DatagramSocket::send ( const struct iovec * iov,
int iovcnt,
int flags = MSG_NOSIGNAL )
inlineinherited

◆ send() [3/3]

size_t nidas::util::DatagramSocket::send ( const void * buf,
size_t len,
int flags = 0 )
inlineinherited

◆ sendto() [1/2]

size_t nidas::util::DatagramSocket::sendto ( const struct iovec * iov,
int iovcnt,
int flags,
const SocketAddress & to )
inlineinherited

◆ sendto() [2/2]

size_t nidas::util::DatagramSocket::sendto ( const void * buf,
size_t len,
int flags,
const SocketAddress & to )
inlineinherited

◆ setBroadcastEnable()

void nidas::util::DatagramSocket::setBroadcastEnable ( bool val)
inlineinherited

◆ setInterface() [1/2]

void nidas::util::MulticastSocket::setInterface ( Inet4Address iaddr)
inline

Set the interface for a given multicast address.

This uses the default value of INADDR_ANY for the specific interface, which causes the system to choose what it thinks is the most appropriate interface, typically the first ethernet interface on the system. If you have more than one candidate interface, you can add an entry in the routing table to indicate which you want to use: route add -host 239.0.0.10 dev eth1

Exceptions
IOException

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::setInterface().

◆ setInterface() [2/2]

void nidas::util::MulticastSocket::setInterface ( Inet4Address maddr,
const Inet4NetworkInterface & iface )
inline

Set the interface for a given multicast address.

If you are sending packets on this MulticastSocket, and do not use setInterface() specific interface, the system will send packets out on the first interface that it finds that is capable of MULTICAST. See setInterface(Inet4Address maddr);

If there are no multicast interfaces that are UP, then the system may not choose the loopback interface by default. So doing multicast on a DHCP laptop that isn't connected to the net may give problems.

Exceptions
IOException

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::setInterface().

◆ setMulticastLoop()

void nidas::util::MulticastSocket::setMulticastLoop ( bool val)
inline

Whether to set the IP_MULTICAST_LOOP socket option.

According to "man 7 ip", IP_MULTICAST_LOOP controls "whether sent multicast packets should be looped back to the local sockets." This is the default in Linux in that setting it does not seem to be necessary for a process on a host to receive multicast packets that are sent out on one of its interfaces, providing the multicast reader has joined that interface, and a firewall is not blocking them (a source of frustration!).

Exceptions
IOException

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::setTimeToLive().

◆ setNonBlocking()

void nidas::util::DatagramSocket::setNonBlocking ( bool val)
inlineinherited

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

Parameters
valtrue=set O_NONBLOCK, false=unset O_NONBLOCK.
Exceptions
IOException

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::setNonBlocking().

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

◆ setPktInfo()

void nidas::util::DatagramSocket::setPktInfo ( bool val)
inlineinherited

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
valif true enable the IP_PKTINFO message, if false, disable.
Exceptions
IOException

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::setPktInfo().

◆ setReceiveBufferSize()

void nidas::util::DatagramSocket::setReceiveBufferSize ( int size)
inlineinherited

◆ setSendBufferSize()

void nidas::util::DatagramSocket::setSendBufferSize ( int size)
inlineinherited

◆ setTimeout()

void nidas::util::DatagramSocket::setTimeout ( int val)
inlineinherited

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.

References nidas::util::DatagramSocket::_impl, and nidas::util::SocketImpl::setTimeout().

Referenced by nidas::core::UDPSocketIODevice::read().

◆ setTimeToLive()

void nidas::util::MulticastSocket::setTimeToLive ( int val)
inline

Member Data Documentation

◆ _impl

SocketImpl nidas::util::DatagramSocket::_impl
protectedinherited

Referenced by nidas::util::DatagramSocket::bind(), nidas::util::DatagramSocket::bind(), nidas::util::DatagramSocket::close(), nidas::util::DatagramSocket::connect(), nidas::util::DatagramSocket::connect(), nidas::util::DatagramSocket::connect(), nidas::util::DatagramSocket::DatagramSocket(), nidas::util::DatagramSocket::DatagramSocket(), nidas::util::DatagramSocket::DatagramSocket(), findInterface(), nidas::util::DatagramSocket::getBroadcastEnable(), nidas::util::DatagramSocket::getFd(), getInterface(), nidas::util::DatagramSocket::getInterfaces(), getInterfaces(), nidas::util::DatagramSocket::getLocalPort(), nidas::util::DatagramSocket::getLocalSocketAddress(), getMulticastLoop(), nidas::util::DatagramSocket::getReceiveBufferSize(), nidas::util::DatagramSocket::getSendBufferSize(), getTimeToLive(), nidas::util::DatagramSocket::isNonBlocking(), joinGroup(), joinGroup(), leaveGroup(), leaveGroup(), nidas::util::DatagramSocket::receive(), nidas::util::DatagramSocket::receive(), nidas::util::DatagramSocket::recv(), nidas::util::DatagramSocket::recvfrom(), nidas::util::DatagramSocket::send(), nidas::util::DatagramSocket::send(), nidas::util::DatagramSocket::send(), nidas::util::DatagramSocket::sendto(), nidas::util::DatagramSocket::sendto(), nidas::util::DatagramSocket::setBroadcastEnable(), setInterface(), setInterface(), setMulticastLoop(), nidas::util::DatagramSocket::setNonBlocking(), nidas::util::DatagramSocket::setPktInfo(), nidas::util::DatagramSocket::setReceiveBufferSize(), nidas::util::DatagramSocket::setSendBufferSize(), nidas::util::DatagramSocket::setTimeout(), and setTimeToLive().


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