Results 1 to 5 of 5

Thread: QUdpSocket understanding

  1. #1
    Join Date
    Sep 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default QUdpSocket understanding

    Hi all,

    I have written a simple protocol using QUdpSocket. The protocol needs to broadcast
    some data to the multicast address 239.0.0.104 - port 65000 and, at the same time,
    receive data from any peers over the network which transmit to the same multicast
    address and port. The sender and receiver functions are independent, that is both
    functions may begin to work without any synchronism.

    To implement the protocol I have used only one socket which is bound to the port
    65000 and belonging to the group 239.0.0.104 to receive datagrams and, when it
    needs to transmit, the socket uses the group 239.0.0.104 as destination address
    and the port 65000 as destination port.

    The problem I have concerns the sending of datagrams. If the sender function starts
    to work before the receiver one, datagrams are not sent over the network but
    only to my application since it's listening on the same port. But, if the receiver
    function starts to work before the sender one, I'm able to transmit datagrams.

    I have detected this behaviour using Qt 4.7.3 and a PC hosting Windows CE 5.0. If
    I port the application on a PC hosting Windows XP/Vista, I have no problem sending
    datagrams also if I have not still received anything.

    Thank you for your cooperation

    Best Regards

    /Alessandro

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QUdpSocket understanding

    In UDP there is no concept of "listening" on sockets so I'm not sure what you mean by this. Could we see some code responsible for socket communication?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QUdpSocket understanding

    You need to use two QUDPSocket, one for Tx and another for Rx.

    Rx UPD Socket:
    - Bind to IP (Local IP) and Port (65000)
    - Connect to data ready signal and read the data

    Tx UPD Socket:
    - Dot not bind (not required to send)
    - Just send data to IP (239.0.0.104) and Port (65000)

  4. #4
    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 08:31. Reason: missing [code] tags

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QUdpSocket understanding

    You have to get rid of the forever loop, you are blocking events and thus network traffic can't be processed by Qt and you won't be getting any datagrams. I'm suprised you are getting any datagrams at all.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Understanding RGB888
    By scarleton in forum Qt Programming
    Replies: 6
    Last Post: 29th August 2010, 21:03
  2. Having trouble understanding brush
    By feraudyh in forum Newbie
    Replies: 4
    Last Post: 30th July 2010, 19:18
  3. Understanding highlighting in a QTableView
    By scarleton in forum Qt Programming
    Replies: 7
    Last Post: 11th July 2010, 14:32
  4. I need help understanding QGraphicsView
    By aarelovich in forum Qt Programming
    Replies: 13
    Last Post: 22nd July 2009, 21:02
  5. Am I not understanding QPixmaps? SOLVED!
    By bjh in forum Qt Programming
    Replies: 0
    Last Post: 18th September 2008, 18: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.