Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

NetUdp : Provide Udp Socket fully exposed to Qml.



  • NetUdp provide a Udp Socket that can send and receive Datagram.

    Code is available here. Full documentation is inside readme.

    Features

    • Support Unicast/Multicast/Broadcast Datagram.
    • C++ and Qml API.
    • Watcher that restart internal socket if something went wrong with the operating system.
      • Retry binding to local port periodically if it failed.
      • Watch network interfaces connection/disconnection to subscribe to multicast groups
      • Send multicast datagram to every network interfaces by default, not only the default one.
    • Worker can optionnaly be moved in a worker thread to perform serialization/deserialization/crc checks etc ...
    • Ready to be inherited for more complex behavior
    • Provide counter of rx/tx data (number of packets, number of bytes sent in total, current bandwidth in bytes/s, ...)

    Qml Usage

    • net::udp::registerQmlTypes(); should be called in the main to register qml types.

    Unicast Datagram

    This example show how to send a unicast datagram as a string to 127.0.0.1:9999. Don't forget to start the socket before sending any messages.

    import QtQuick 2.0
    import QtQuick.Controls 2.0
    
    import NetUdp 1.0 as NetUdp
    
    Button
    {
      text: "send unicast"
      onClicked: () => socket.sendDatagram({
        address: "127.0.0.1",
        port: 9999,
        data: "My Data"
        // Equivalent to 'data: [77,121,32,68,97,116,97]'
      })
    
      NetUdp.Socket
      {
        id: socket
        Component.onCompleted: () => start()
      }
    }
    

    This example show how to receive the datagram. Don't forget to start listening to an address and a port. The datagram is always received as a string. It can easily be decoded to manipulate a byte array.

    import NetUdp 1.0 as NetUdp
    
    NetUdp.Socket
    {
      onDatagramReceived: function(datagram)
      {
        console.log(`datagram : ${JSON.stringify(datagram)}`)
        console.log(`datagram.data (string) : "${datagram.data}"`)
        let byteArray = []
        for(let i = 0; i < datagram.data.length; ++i)
          byteArray.push(datagram.data.charCodeAt(i))
        console.log(`datagram.data (bytes): [${byteArray}]`)
          
        console.log(`datagram.destinationAddress : ${datagram.destinationAddress}`)
        console.log(`datagram.destinationPort : ${datagram.destinationPort}`)
        console.log(`datagram.senderAddress : ${datagram.senderAddress}`)
        console.log(`datagram.senderPort : ${datagram.senderPort}`)
        console.log(`datagram.ttl : ${datagram.ttl}`)
      }
    
      Component.onCompleted: () => start("127.0.0.1", 9999)
    }
    

    Multicast Datagram

    Send multicast datagram work almost the same as unicast. Only difference is that you control on which interface the data is going.

    import NetUdp 1.0 as NetUdp
    
    NetUdp.Socket
    {
      id: socket
      // A Packet will be send to each interface
      // The socket monitor for interface connection/disconnection
      multicastOutgoingInterfaces: [ "lo", "eth0" ]
      // Required in unix world if you want loopback on the same system
      multicastLoopback: true
        
      Component.onCompleted: () => start()
    }
    

    Then send data like in unicast:

    socket.sendDatagram({
      address: "239.1.2.3",
      port: 9999,
      data: "My Data"
    })
    

    To receive it, subscribe the to the multicast group and choose on which interfaces.

    import NetUdp 1.0 as NetUdp
    
    NetUdp.Socket
    {
      multicastGroups: [ "239.1.3.4" ]
      multicastListeningInterfaces: [ "lo", "eth0" ]
        
      // Required in the windows world if you want loopback on the same system
      multicastLoopback: true
    
      onDatagramReceived: (datagram) => console.log(`datagram : ${JSON.stringify(datagram)}`)
    
      // Listen port 9999
      Component.onCompleted: () => start(12999934)
    }
    

    Feel free to improve/fork/submit ideas.


  • Lifetime Qt Champion

    Thank you for sharing ❤


Log in to reply