00001
00002
00003
00004
00005
00006
00007
00008
00010
00011 #ifndef __MULTICAST_H__
00012 #define __MULTICAST_H__
00013
00014
00015 #include <qsocketdevice.h>
00016
00017
00018 #include "compat.h"
00019
00022
00023
00024
00027
00028
00029
00030 class QMulticastSocket : public QSocketDevice
00031 {
00032 public:
00033
00034 QHostAddress m_address;
00035 Q_UINT16 m_port;
00036 struct ip_mreq m_imr;
00037
00038 public:
00039
00040 QMulticastSocket( QString sAddress, Q_UINT16 nPort, u_char ttl = 0 )
00041 : QSocketDevice( QSocketDevice::Datagram )
00042 {
00043 m_address.setAddress( sAddress );
00044 m_port = nPort;
00045
00046 if (ttl == 0)
00047 ttl = 4;
00048
00049
00050
00051 m_imr.imr_multiaddr.s_addr = inet_addr( sAddress );
00052 m_imr.imr_interface.s_addr = htonl(INADDR_ANY);
00053
00054 if ( setsockopt( socket(), IPPROTO_IP, IP_ADD_MEMBERSHIP, &m_imr, sizeof( m_imr )) < 0)
00055 {
00056 VERBOSE(VB_IMPORTANT, QString( "QMulticastSocket: setsockopt - IP_ADD_MEMBERSHIP Error" ));
00057 }
00058
00059 setsockopt( socket(), IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl) );
00060
00061 setAddressReusable( true );
00062
00063 bind( m_address, m_port );
00064 }
00065
00066 virtual ~QMulticastSocket()
00067 {
00068 setsockopt( socket(), IPPROTO_IP, IP_DROP_MEMBERSHIP, &m_imr, sizeof(m_imr));
00069 }
00070 };
00071
00072 #endif