Results 1 to 10 of 10

Thread: IPC using UDP multicast

  1. #1
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Question IPC using UDP multicast

    Hello folks,

    I have a question about multicasting using QUdpSocket.

    I am required to implement an event based IPC mechanism. One process should signal events, arbitrary other processes should be able to receive those.
    My idea would be to implement a multicast sender to post events and a multicast receiver to receive those events. The message
    to be transmitted itself would be serialized using QDataStream with the following byte stream format:

    quint(nr of bytes to send) QString(channel name) QString(message name) QByteArray(payload)

    Can there occur a problem if the multicast sender transmits the data stream and a client joins the group somewhere in time after the server has sent the first
    bytes and before all bytes have been sent? In other words, does the client need to synchronize somehow on the received data?

    thanks in advance,
    michael

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: IPC using UDP multicast

    This is purely dependent on what your message mean, but if you require messages to be seen then UDP might not be a good choice since there is no guarantee that all packets will arrive or arrive in the order they were sent.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: IPC using UDP multicast

    Thanks for your reply!

    I thought of using UDP due to the possibility of using multicasts - as the IPC project LCM does [1]

    By 'message' I mean simply serialized QObjects, e.g. an error object should be sent and received via a dedicated error channel.

    Do you know any other technology for implementing a one-to-many communication instead of UDP multicasting?

    Best regards,
    michael


    [1] https://lcm-proj.github.io/

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: IPC using UDP multicast

    On an IP based network you have two options, UDP and TCP.

    If you need packets to arrive with certainty or require ordering, then you either implement that on top of UDP yourself or use TCP.

    As for multicast, the main difference between an UDP multicast and a TCP server sending the same messages to multiple clients is that the in the latter case the server needs to have connections to each client, while a UDP multicaster doesn't need to know if or how many are listening.

    Cheers,
    _

  5. #5
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: IPC using UDP multicast

    Ok, I see, thank you very much.

    Can we summarize the following:

    Assuming I want to send a serialized QObject (just a series of bytes) using QUdpSocket, this byte stream could be split by the UDP transport layer into single UDP datagrams (e.g. because there are too many bytes in the payload). The receiver then has no guarantee that it can de-serialize (using QDataStream) the received data into the same QObject which has been sent, because the datagrams might be received re-ordered?

    Best regards,
    michael

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: IPC using UDP multicast

    No, I don't think so.

    If I remember correctly a UDP datagram either arrives full or not, even when it has been fragmented by a lower layer.

    But of course if the application has to split something into multiple UDP datagrams, then any of them might not arrive or out of order.

    Cheers,
    _

  7. #7
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: IPC using UDP multicast

    Ok, so if I simply write my object as 'one' datagram, the subscriber will be able to deserialize the bytes received.

    Qt Code:
    1. class Publisher : public QUdpSocket {
    2.  
    3. void publishEvent(MyEvent const& e) {
    4. QByteArray eventData;
    5. QDataStream out(&eventData, QIODevice::WriteOnly);
    6. out.setVersion(QDataStream::Qt_4_8);
    7.  
    8. // Add request-dependent data
    9. out<< e;
    10.  
    11. writeDatagram(eventData.size(), eventData);
    12. }
    13. }
    14.  
    15. class Subscriber : public QUdpSocket {
    16.  
    17. void onReadyRead() {
    18. QByteArray eventData;
    19.  
    20. do {
    21. eventData.resize(pendingDatagramSize());
    22. readDatagram(eventData.data(), eventData.size());
    23.  
    24. QDataStream in(&eventData, QIODevice::ReadOnly);
    25.  
    26. // Get and handle received event data
    27. MyEventData e;
    28. in>> e;
    29.  
    30. } while (hasPendingDatagrams());
    31. }
    To copy to clipboard, switch view to plain text mode 

    The doc for QUdpSocket::writeDatagram() states that the number of bytes to be sent should not be larger than 512 bytes because in IP layer the data could be fragmented. But isn't the IP layer on the receiver side responsible for reassembilng the data and providing the data for the UDP layer as one datagram? IMHO if due to fragmentation a single IP packet is getting lost, the UDP datagram can not be reassembled and therefore the whole datagram will be lost.

    best regards,
    michael

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: IPC using UDP multicast

    The maximum packet size of IP is 65535 (16bit length field).

    Ethernet often has 1500 byte MTU (maximum transfer unit), other technologies have smaller or larger sizes.

    Since an IP packet can cross a lot of channels with different size requirements, a single IP packet might be split and reassembled quite a lot along its route.

    If, for whatever reason, part of a UDP packet is lost, it can no longer be reassembled by the receiving IP stack and will be discarded.

    Since UDP has not retransmit or assumption that packets will arrive this is generally OK, the transmission partners have to deal with packet loss anyway.

    The larger a packet is, the higher the chance that it gets fragmented and the more fragments, the higher the chance that one is lost.

    The latter chance of course depends a lot on the transmission path.
    E.g. in a local ethernet this is not likely to happen at all.

    Cheers,
    _

  9. The following user says thank you to anda_skoa for this useful post:

    MrGentleman (29th August 2016)

  10. #9
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Thumbs up Re: IPC using UDP multicast

    Thank you very much for your input!
    Since the IPC mechanism is used only locally on the host machine and the number of bytes to send/receive are less than 200 I will implement
    a publisher/subscriber system based on UDP multicast.

    Best regards,
    michael

  11. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: IPC using UDP multicast

    On the loopback device there shouldn't be any problem with packet loss (aside from receiver buffers running full), reordering or packet fragmentation

    Cheers,
    _

Similar Threads

  1. UDP Multicast
    By Thomas_Lerman in forum Qt Programming
    Replies: 0
    Last Post: 29th July 2014, 20:51
  2. Multicast in Qt -- Help needed
    By swamyonline in forum Qt Programming
    Replies: 1
    Last Post: 27th November 2008, 10:30
  3. Does Qt 4.2 support multicast?
    By zeki709 in forum Qt Programming
    Replies: 2
    Last Post: 10th February 2007, 16:00
  4. MULTICAST with QT 4.1 and above.
    By jlarsj in forum Qt Programming
    Replies: 7
    Last Post: 10th January 2007, 12:45
  5. UDP multicast with Qt
    By madcat in forum Qt Programming
    Replies: 0
    Last Post: 25th April 2006, 21:31

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.