1

I am trying to receive packet from UDP-connected infrared camera. (FLIR Lepton 2.5) And I'm trying to run my codes on Windows 10, Qt Creator 4.5, Qt 5.9.

As you can see on the capture below, my UDP-connected infrared camera sends UDP packets.

enter image description here

And here's my code:

// myudp.cpp

#include "myudp.h"
#include <QNetworkProxy>

MyUDP::MyUDP(QObject *parent) :
    QObject(parent)
{
    // create a QUDP socket
    socket = new QUdpSocket(this);
    socket_cam = new QUdpSocket(this);

    cam_address = QHostAddress("192.168.10.197");
    cam_port = 32197;

    connect(socket, SIGNAL(connected()), this, SLOT(readNwrite()));

    socket->connectToHost(cam_address, cam_port, QIODevice::ReadWrite);

    if (socket->waitForConnected(2000))
        qDebug("Connected!");
    else
        qDebug("Cannot be connected..");
    qDebug() << socket->state();
//    seems to be connected...
}

void MyUDP::readNwrite()
{
    QByteArray data;
    qint64 resize_size = socket->pendingDatagramSize();
    //socket have NO size(resize_size == -1, ERROR!)
    if(resize_size != -1)
    {
        qDebug() << "data was resized properly; size: "<<resize_size;
        data.resize(socket->pendingDatagramSize());
    }
    else
        qDebug() << "data could not be resized(error)";
    qint64 det_num;
    det_num = socket->readDatagram(data.data(), 964, &cam_address, &cam_port);
    qDebug() << "Can receive data(returns -1 if NO): "<<det_num;
    //this returns nothing, too!
    qDebug() << "Data is here: " << data.data();

}

Here's my code implementation result:

When signal is connected(), data could not be resized(error)
Can receive data(returns -1 if NO): -1
Data is here:
Connected!
QAbstractSocket::ConnectedState

When signal is readyRead(), Connected!
QAbstractSocket::ConnectedState data was resized properly; size: 0
Can receive data(returns -1 if NO): -1 Data is here:
readyRead signal sometimes seems NOT emitted as expected.

But it is obvious that any data is NOT being sent.

I've tried to find examples which are related to UDP network with a remote network. But there were few of them.

According to some threads, people recommended not to use connectToHost function on UDP network. However, I don't know how to make an approach to remote network without using connectToHost.

I want to know how I should correct my code to get packet from a remote network. Any advice will be very grateful for me because I'm a newbie to UDP network, and Qt.

Mike
  • 14,010
  • 29
  • 101
  • 161
Jinhyeok
  • 21
  • 3
  • 3
    The camera is sending it's video frames through UDP broadcast .. you don't want to connect to the camera's IP, as that would just be used to send UDP packets _to_ the camera, you want to essentially listen to `255.255.255.255` .. in other words, you need to change your code to connect to a UDP multi-cast address and receive the packets that way. – txtechhelp Jan 16 '19 at 09:11
  • 1
    dont know much qt either, but udp does not really have connections, you just receive a package and then maybe more, but unlike tcp there is no connection established – 463035818_is_not_an_ai Jan 16 '19 at 09:12
  • @txtechhelp Thanks for giving advice to me. Does it mean that I need to listen only 255.255.255.255 instead of camera's IP? I actually don't understand why I need UDP multi-cast network... Would you explain more about it? – Jinhyeok Jan 16 '19 at 12:45
  • @user463035818 Thanks for advices, I learned that UDP don't need connection. I think I need to correct my code. :) – Jinhyeok Jan 16 '19 at 12:54
  • "broadcast" and "multicast" are two different things in network programming. The camera is sending an IPv4 subnet "broadcast". All you have to do is bind a UDP socket to a local NIC that is connected to the same subnet as the camera, and then start reading from the socket. You *can* `connect` a UDP socket, but it is optional. It establishes a static association for the local socket to a remote address so the socket will receive packets from, and send packets to, only that "connected" address and no others. It is not a true 2way connection, like in TCP, it is more like a filtering mechanism. – Remy Lebeau Jan 16 '19 at 19:51
  • Either way, there is no `connected` state/signal in UDP, so don't wait for one. – Remy Lebeau Jan 16 '19 at 19:51
  • @Remy Lebeau Thanks for your advice. I rewrote codes which use multicast. However, I bound the socket to LocalHost and joined multicast on 255.255.255.255. As the return value of joinmulticast funtion, false was returned. (According to qt documents, it means it failed to multicast.) After this, I pinged 255.255.255.255 on cmd and said that the host is NOT found. Is this direction different from what you meant on the previous comments? – Jinhyeok Jan 18 '19 at 05:18
  • @Jinhyeok 255.255.255.255 is not a multicast address, it is a subnet broadcast address, so trying to use multicast is wrong in this situation. Just so what I said earlier (bind to the NIC attached to the camera's subnet, not to localhost, then read), it should work fine – Remy Lebeau Jan 18 '19 at 16:22

0 Answers0