Problem to bind a UDP socket.
-
@jenya7 Please connect a slot to https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred and print the error there.
-
@KroMignon said in Problem to bind a UDP socket.:
@jenya7 said in Problem to bind a UDP socket.:
result = socket->bind(QHostAddress(local_ip), local_port); QString err = socket->errorString(); result = socket->error();
This code do not make sense to me, you have to check if operation was not successful before trying to find an error!
There is no error code "Success" inQAbstractSocket::SocketError
(cf. https://doc.qt.io/qt-5/qabstractsocket.html#SocketError-enum).Please review your code.
OK.
int UDP::Start(QString local_ip, quint16 local_port, QString *err_str) { int result; //socket->reset(); //socket->setProxy(QNetworkProxy::NoProxy); result = socket->bind(QHostAddress::Any, local_port); //result = socket->bind(QHostAddress(local_ip), local_port); if (result != 0) { *err_str = socket->errorString(); result = socket->error(); } return result; }
So I get result = 1 after result = socket->bind(QHostAddress::Any, local_port);
Should I trust to a returned result? -
@jsulm said in Problem to bind a UDP socket.:
@jenya7 Please connect a slot to https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred and print the error there.
can not do that.
it's UDP socket, not Abstract socket. it has no errorOccurred signal.QUdpSocket *socket;
-
@jenya7 said in Problem to bind a UDP socket.:
So I get result = 1 after result = socket->bind(QHostAddress::Any, local_port);
Should I trust to a returned result?Please take time to read a little bit of documentation. This will save you many of your time. It is very easy with Qt Creator, you only have to hit F1 to get help for a function!
QUdpSocket::bind()
(cf. https://doc.qt.io/qt-5/qabstractsocket.html#bind) will return a boolean:true
: successfalse
: error
-
@jenya7 said in Problem to bind a UDP socket.:
it's UDP socket, not Abstract socket.
QUdpSocket derives from QAbstractSocket...
udp_1.Start("192.168.3.40", 8001, &err_str);
You call Start() with an IP which you computer doesn't have according your ipconfig output above. This can't work. Also 172.16.1.118 is not your IP.
-
@KroMignon said in Problem to bind a UDP socket.:
@jenya7 said in Problem to bind a UDP socket.:
it's UDP socket, not Abstract socket. it has no errorOccurred signal.
QUpdSocket
is based onQAbstractSocket
, do you have C++ knowledge?
Do you know what subclassing means?QObject::connect(socket, &QAbstractSocket::error, this, &UDP::SocketError); void UDP::SocketError(QAbstractSocket::SocketError socketError) { qDebug() << "Socket Error: " << socketError; }
I get
error: no matching member function for call to 'connect' -
@jenya7 said in Problem to bind a UDP socket.:
QObject::connect(socket, &QAbstractSocket::error, this, &UDP::SocketError);
And again to lazy to hit 'F1': https://doc.qt.io/qt-5/qabstractsocket-obsolete.html#error-1
Note: Signal error is overloaded in this class. To connect to this signal by using the function pointer syntax, Qt provides a convenient helper for obtaining the function pointer as shown in this example:
connect(abstractSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), [=](QAbstractSocket::SocketError socketError){ /* ... */ });
Also this signal is deprecated - use errorOccurred() if possible (since Qt5.15)
-
@jenya7 said in Problem to bind a UDP socket.:
QObject::connect(socket, &QAbstractSocket::error, this, &UDP::SocketError);
This signal is called
errorOccurred()
and noterror()
[EDIT]
errorOccurred()
has been added with Qt 5.15, if you are using an older Qt version, use code suggested by @Christian-Ehrlicher -
@KroMignon said in Problem to bind a UDP socket.:
@jenya7 said in Problem to bind a UDP socket.:
So I get result = 1 after result = socket->bind(QHostAddress::Any, local_port);
Should I trust to a returned result?Please take time to read a little bit of documentation. This will save you many of your time. It is very easy with Qt Creator, you only have to hit F1 to get help for a function!
QUdpSocket::bind()
(cf. https://doc.qt.io/qt-5/qabstractsocket.html#bind) will return a boolean:true
: successfalse
: error
oops. sorry.