Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QUdpSocket binding issue
Forum Updated to NodeBB v4.3 + New Features

[Solved] QUdpSocket binding issue

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Richard Lee
    wrote on last edited by Richard Lee
    #1

    I have been using Qt for desktop applications programming for a quite long time, but I am totally a newbie in network programmings. Recently I am developing a program to remotely control my Sony camera via WIFI.

    According to the official API document, I need to use SSDP to discover the camera and get the LOCATION, which is an address where API commands sent. I use a QUdpSocket object to send SSDP request, but I never got complete reply from the server. This problem has puzzled me for a couple of days. Fortunately, finally I fixed it by myself. I post it here. Hope someone who are also as newbie as I know how to do when encountering the same problem. Below is the description:

    My first attempt is this:

    QHostAddress hostAddress = QHostAddress("239.255.255.250");
    int SSDP_HOST = 1900;
    QUdpSocket *udpSocket = new QUdpSocket(this);
    udpSocket->bind(hostAddress, SSDP_PORT);
    udpSocket->joinMulticastGroup(hostAddress);
    connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
    

    By doing this way, I will never get complete reply from the host. Like this:

    SSDP request M-SEARCH * HTTP/1.1
    HOST: 239.255.255.250:1900
    MAN: "ssdp:discover"
    MX: 1
    ST: urn:schemas-sony-com:service:ScalarWebAPI:1

    At the beginning, I totally have no idea how to debug. I thought I missed some step to setup the network configuration, but actually it was not. Then I looked the example provided in the help document, and found in the example code, the udpSocket did not bind to the host address. So I revised my code as this:

    QHostAddress hostAddress = QHostAddress("239.255.255.250");
    int SSDP_HOST = 1900;
    QUdpSocket *udpSocket = new QUdpSocket(this);
    udpSocket->bind(QHostAddress::AnyIPv4, SSDP, QUdpSocket::SharedAddress);
    udpSocket->joinMulticastGroup(hostAddress);
    connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
    

    Then I got full response from the server (camera):
    NOTIFY * HTTP/1.1
    HOST: 239.255.255.250:1900
    CACHE-CONTROL: max-age=1800
    LOCATION: http://192.168.122.1:61000/scalarwebapi_dd.xml
    NT: upnp:rootdevice
    NTS: ssdp:alive
    SERVER: FedoraCore/2 UPnP/1.0 MINT-X/1.8.1
    USN: uuid:000000001000-1010-8000-B272BF6D76E5::upnp:rootdevice

    In conclusion, the lesson learned from this is this: If you want to use the same QUdpSocket object to send and receive UDP datagram to the client and from the server, you need to bind the QUdpSocket object to a different IP address, but the same port of the server's. Otherwise, you cannot get full reply.

    Hope this could help others. Thanks.

    SGaistS 1 Reply Last reply
    1
    • sonichyS Offline
      sonichyS Offline
      sonichy
      wrote on last edited by
      #2

      Where is the sample code ?

      https://github.com/sonichy

      1 Reply Last reply
      0
      • R Richard Lee

        I have been using Qt for desktop applications programming for a quite long time, but I am totally a newbie in network programmings. Recently I am developing a program to remotely control my Sony camera via WIFI.

        According to the official API document, I need to use SSDP to discover the camera and get the LOCATION, which is an address where API commands sent. I use a QUdpSocket object to send SSDP request, but I never got complete reply from the server. This problem has puzzled me for a couple of days. Fortunately, finally I fixed it by myself. I post it here. Hope someone who are also as newbie as I know how to do when encountering the same problem. Below is the description:

        My first attempt is this:

        QHostAddress hostAddress = QHostAddress("239.255.255.250");
        int SSDP_HOST = 1900;
        QUdpSocket *udpSocket = new QUdpSocket(this);
        udpSocket->bind(hostAddress, SSDP_PORT);
        udpSocket->joinMulticastGroup(hostAddress);
        connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
        

        By doing this way, I will never get complete reply from the host. Like this:

        SSDP request M-SEARCH * HTTP/1.1
        HOST: 239.255.255.250:1900
        MAN: "ssdp:discover"
        MX: 1
        ST: urn:schemas-sony-com:service:ScalarWebAPI:1

        At the beginning, I totally have no idea how to debug. I thought I missed some step to setup the network configuration, but actually it was not. Then I looked the example provided in the help document, and found in the example code, the udpSocket did not bind to the host address. So I revised my code as this:

        QHostAddress hostAddress = QHostAddress("239.255.255.250");
        int SSDP_HOST = 1900;
        QUdpSocket *udpSocket = new QUdpSocket(this);
        udpSocket->bind(QHostAddress::AnyIPv4, SSDP, QUdpSocket::SharedAddress);
        udpSocket->joinMulticastGroup(hostAddress);
        connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
        

        Then I got full response from the server (camera):
        NOTIFY * HTTP/1.1
        HOST: 239.255.255.250:1900
        CACHE-CONTROL: max-age=1800
        LOCATION: http://192.168.122.1:61000/scalarwebapi_dd.xml
        NT: upnp:rootdevice
        NTS: ssdp:alive
        SERVER: FedoraCore/2 UPnP/1.0 MINT-X/1.8.1
        USN: uuid:000000001000-1010-8000-B272BF6D76E5::upnp:rootdevice

        In conclusion, the lesson learned from this is this: If you want to use the same QUdpSocket object to send and receive UDP datagram to the client and from the server, you need to bind the QUdpSocket object to a different IP address, but the same port of the server's. Otherwise, you cannot get full reply.

        Hope this could help others. Thanks.

        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        @Richard-Lee said in [Solved] QUdpSocket binding issue:

        At the beginning, I totally have no idea how to debug. I thought I missed some step to setup the network configuration, but actually it was not. Then I looked the example provided in the help document, and found in the example code, the udpSocket did not bind to the host address. So I revised my code as this:
        QHostAddress hostAddress = QHostAddress("239.255.255.250");
        int SSDP_HOST = 1900;
        QUdpSocket *udpSocket = new QUdpSocket(this);
        udpSocket->bind(QHostAddress::AnyIPv4, SSDP, QUdpSocket::SharedAddress);
        udpSocket->joinMulticastGroup(hostAddress);
        connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));

        If you mean the Qt Documentation example, they are all linked in the QUdpSocket documentation details.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        sonichyS 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          @Richard-Lee said in [Solved] QUdpSocket binding issue:

          At the beginning, I totally have no idea how to debug. I thought I missed some step to setup the network configuration, but actually it was not. Then I looked the example provided in the help document, and found in the example code, the udpSocket did not bind to the host address. So I revised my code as this:
          QHostAddress hostAddress = QHostAddress("239.255.255.250");
          int SSDP_HOST = 1900;
          QUdpSocket *udpSocket = new QUdpSocket(this);
          udpSocket->bind(QHostAddress::AnyIPv4, SSDP, QUdpSocket::SharedAddress);
          udpSocket->joinMulticastGroup(hostAddress);
          connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));

          If you mean the Qt Documentation example, they are all linked in the QUdpSocket documentation details.

          sonichyS Offline
          sonichyS Offline
          sonichy
          wrote on last edited by
          #4

          @SGaist I mean SSDP sampple code !

          https://github.com/sonichy

          SGaistS 1 Reply Last reply
          0
          • sonichyS sonichy

            @SGaist I mean SSDP sampple code !

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @sonichy well, they did post what he used to do the connection and I quoted it as well.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved