Results 1 to 5 of 5

Thread: QUdpSocket understanding

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Sep 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QUdpSocket understanding

    Here is the code I'm using:

    Qt Code:
    1. lOldFlags = 0;
    2.  
    3. /*
    4. ** Forever loop
    5. */
    6. for (;;)
    7. {
    8. msleep (100);
    9.  
    10. /*
    11.   ** Process any datagrams in the queue
    12.   */
    13. while (cSocket.hasPendingDatagrams ())
    14. {
    15. lDgram.resize (cSocket.pendingDatagramSize ());
    16. cSocket.readDatagram (lDgram.data (), lDgram.size (), &lPeer, &lPort);
    17.  
    18. /* ... process the datagram ... */
    19. }
    20.  
    21.  
    22. /*
    23.   ** Check for unplugged network cable. When the network cable is
    24.   ** unplugged and plugged again the socket stops receiving datagrams.
    25.   ** The following code detects the event and calls "bind". This is the
    26.   ** point where the socket is bound the first time because lOldFlags is
    27.   ** initialize to zero
    28.   */
    29. QNetworkInterface lIf (QNetworkInterface::interfaceFromName (cAdapterName));
    30. if (lOldFlags != lIf.flags ()) /* ... if there has been any change in the interface status ... */
    31. {
    32. lOldFlags = lIf.flags ();
    33. if (lIf.flags () & 0x02) /* ... if the interface is running ... */
    34. {
    35. cSocket.disconnectFromHost ();
    36. #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
    37. if (!cSocket.bind (QHostAddress ("0.0.0.0"), 65000, QUdpSocket::ReuseAddressHint))
    38. #else
    39. if (!cSocket.bind (QHostAddress ("0.0.0.0"), 65000, QUdpSocket::ShareAddress))
    40. #endif
    41. lOldFlags = 0;
    42. else
    43. {
    44. int lTTL = 1;
    45. ip_mreq lMReq;
    46. memset ((void *) &lMReq, 0, sizeof (lMReq));
    47. lMReq.imr_multiaddr.s_addr = _HTONL (QHostAddress ("239.0.0.104").toIPv4Address ());
    48. lMReq.imr_interface.s_addr = _HTONL (QHostAddress ("0.0.0.0").toIPv4Address ());
    49. if (setsockopt (cSocket.socketDescriptor (), IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char *) &lMReq, sizeof (lMReq)) < 0) lOldFlags = 0;
    50. if (setsockopt (cSocket.socketDescriptor (), IPPROTO_IP, IP_MULTICAST_TTL, (const char *) &lTTL, sizeof (lTTL)) < 0) lOldFlags = 0;
    51. }
    52. }
    53. }
    54.  
    55.  
    56. /*
    57.   ** If there are the conditions write a datagram ...
    58.   */
    59. /* ... some code here to detect the conditions ... */
    60. cSocket.writeDatagram (lMsg.data (), lMsg.size (), QHostAddress ("239.0.0.104"), 65000);
    61.  
    62. } /* end forevere loop */
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 7th July 2011 at 07:31. Reason: missing [code] tags

Similar Threads

  1. Understanding RGB888
    By scarleton in forum Qt Programming
    Replies: 6
    Last Post: 29th August 2010, 20:03
  2. Having trouble understanding brush
    By feraudyh in forum Newbie
    Replies: 4
    Last Post: 30th July 2010, 18:18
  3. Understanding highlighting in a QTableView
    By scarleton in forum Qt Programming
    Replies: 7
    Last Post: 11th July 2010, 13:32
  4. I need help understanding QGraphicsView
    By aarelovich in forum Qt Programming
    Replies: 13
    Last Post: 22nd July 2009, 20:02
  5. Am I not understanding QPixmaps? SOLVED!
    By bjh in forum Qt Programming
    Replies: 0
    Last Post: 18th September 2008, 17:34

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.