lOldFlags = 0;
/*
** Forever loop
*/
for (;;)
{
msleep (100);
/*
** Process any datagrams in the queue
*/
while (cSocket.hasPendingDatagrams ())
{
lDgram.resize (cSocket.pendingDatagramSize ());
cSocket.readDatagram (lDgram.data (), lDgram.size (), &lPeer, &lPort);
/* ... process the datagram ... */
}
/*
** Check for unplugged network cable. When the network cable is
** unplugged and plugged again the socket stops receiving datagrams.
** The following code detects the event and calls "bind". This is the
** point where the socket is bound the first time because lOldFlags is
** initialize to zero
*/
if (lOldFlags != lIf.flags ()) /* ... if there has been any change in the interface status ... */
{
lOldFlags = lIf.flags ();
if (lIf.flags () & 0x02) /* ... if the interface is running ... */
{
cSocket.disconnectFromHost ();
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
#else
#endif
lOldFlags = 0;
else
{
int lTTL = 1;
ip_mreq lMReq;
memset ((void *) &lMReq, 0, sizeof (lMReq));
lMReq.
imr_multiaddr.
s_addr = _HTONL
(QHostAddress ("239.0.0.104").
toIPv4Address ());
lMReq.
imr_interface.
s_addr = _HTONL
(QHostAddress ("0.0.0.0").
toIPv4Address ());
if (setsockopt (cSocket.socketDescriptor (), IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char *) &lMReq, sizeof (lMReq)) < 0) lOldFlags = 0;
if (setsockopt (cSocket.socketDescriptor (), IPPROTO_IP, IP_MULTICAST_TTL, (const char *) &lTTL, sizeof (lTTL)) < 0) lOldFlags = 0;
}
}
}
/*
** If there are the conditions write a datagram ...
*/
/* ... some code here to detect the conditions ... */
cSocket.
writeDatagram (lMsg.
data (), lMsg.
size (),
QHostAddress ("239.0.0.104"),
65000);
} /* end forevere loop */
lOldFlags = 0;
/*
** Forever loop
*/
for (;;)
{
msleep (100);
/*
** Process any datagrams in the queue
*/
while (cSocket.hasPendingDatagrams ())
{
lDgram.resize (cSocket.pendingDatagramSize ());
cSocket.readDatagram (lDgram.data (), lDgram.size (), &lPeer, &lPort);
/* ... process the datagram ... */
}
/*
** Check for unplugged network cable. When the network cable is
** unplugged and plugged again the socket stops receiving datagrams.
** The following code detects the event and calls "bind". This is the
** point where the socket is bound the first time because lOldFlags is
** initialize to zero
*/
QNetworkInterface lIf (QNetworkInterface::interfaceFromName (cAdapterName));
if (lOldFlags != lIf.flags ()) /* ... if there has been any change in the interface status ... */
{
lOldFlags = lIf.flags ();
if (lIf.flags () & 0x02) /* ... if the interface is running ... */
{
cSocket.disconnectFromHost ();
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
if (!cSocket.bind (QHostAddress ("0.0.0.0"), 65000, QUdpSocket::ReuseAddressHint))
#else
if (!cSocket.bind (QHostAddress ("0.0.0.0"), 65000, QUdpSocket::ShareAddress))
#endif
lOldFlags = 0;
else
{
int lTTL = 1;
ip_mreq lMReq;
memset ((void *) &lMReq, 0, sizeof (lMReq));
lMReq.imr_multiaddr.s_addr = _HTONL (QHostAddress ("239.0.0.104").toIPv4Address ());
lMReq.imr_interface.s_addr = _HTONL (QHostAddress ("0.0.0.0").toIPv4Address ());
if (setsockopt (cSocket.socketDescriptor (), IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char *) &lMReq, sizeof (lMReq)) < 0) lOldFlags = 0;
if (setsockopt (cSocket.socketDescriptor (), IPPROTO_IP, IP_MULTICAST_TTL, (const char *) &lTTL, sizeof (lTTL)) < 0) lOldFlags = 0;
}
}
}
/*
** If there are the conditions write a datagram ...
*/
/* ... some code here to detect the conditions ... */
cSocket.writeDatagram (lMsg.data (), lMsg.size (), QHostAddress ("239.0.0.104"), 65000);
} /* end forevere loop */
To copy to clipboard, switch view to plain text mode
Bookmarks