Solved A UDP socket problem.
-
@jenya7 Connect a slot to https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred before you call bind and see what the error is (https://doc.qt.io/qt-5/qabstractsocket.html#error or https://doc.qt.io/qt-5/qiodevice.html#errorString)
-
@jsulm
I addedconnect_status = connect(socket, SIGNAL(errorOccurred()), this, SLOT(SocketError())); void MyUDP::SocketError(QAbstractSocket::SocketError socketError) { qDebug() << "Socket Error: " << socketError; }
But I get - QObject::connect: No such signal QUdpSocket::errorOccurred() in udp.cpp:30
I noticed on start up it prints in Application Output -
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QUdpSocket(0x3de510), parent's thread is QThread(0x263437f0), current thread is QThread(0x22fcf0) QNativeSocketEngine::bind() was not called in QAbstractSocket::UnconnectedState -
@jenya7 said in A UDP socket problem.:
connect_status = connect(socket, SIGNAL(errorOccurred()), this, SLOT(SocketError()));
Try
connect_status = connect(socket, &QAbstractSocket::errorOccurred, this, &MyUDP::SocketError);
-
@jenya7 said in A UDP socket problem.:
connect_status = connect(socket, SIGNAL(errorOccurred()), this, SLOT(SocketError()));
You forgot to mention the parameter types.
But, as @mchinand pointed out, you should really switch to "new" Qt5 connect syntax. -
@jsulm
I replacedconnect_status = connect(socket, &QUdpSocket::readyRead, this, &MyUDP::ReadyRead);
but on this one
connect_status = connect(socket, &QAbstractSocket::errorOccurred, this, &MyUDP::SocketError);
I get - error: no member named 'errorOccurred' in 'QAbstractSocket'
-
@jenya7 Ah, from the doc: "This function was introduced in Qt 5.15."
What Qt version do you use?Try to call errorString() after calling bind().
-
connect_status = socket->bind(QHostAddress(local_ip), local_port); QString err = socket->errorString();
err = "Unknown error"
-
@jenya7 What you also can do is: connect a slot to https://doc.qt.io/qt-5/qabstractsocket.html#stateChanged and print out the state and error string from errorString().
-
@jsulm
Placed in constructorMyUDP::MyUDP(QObject *parent) : QObject(parent) { socket = new QUdpSocket(this); QObject::connect(socket, &QUdpSocket::readyRead, this, &MyUDP::ReadyRead); }
Now it binds good.
-
what you are doing is generally a bad idea unless you are aggregating channels for redundancy or to increase badnwidth....in which case you'd have a bridge interface and do your bind() there.
I don't understand why you are doing this in the first place since it complicates network routing, with minimal to no gain.
I'm kind of breaking my own rule about not questioning the "reason" a person asks a question, but I'm at a loss as to why you think you need to do this.
FWIW, you can assign multiple IPs to a single NIC is that is what you need.