Problem to bind a UDP socket.
-
The bind method
int UDP::Start(QString local_ip, quint16 local_port) { int result; //socket->reset(); //socket->setProxy(QNetworkProxy::NoProxy); //result = socket->bind(QHostAddress::Any, local_port); result = socket->bind(QHostAddress(local_ip), local_port); QString err = socket->errorString(); result = socket->error(); return result; }
In main
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); UDP udp_1 ; int soc_err; QString qs; QObject::connect(&udp_1, &UDP::ReadyForReader, &reader, &READER::ReadyForReader); GetNetInfo(); soc_err = udp_1.Start("192.68.3.40", 8001); if (soc_err) { qs = QString::number(soc_err); cout << "Socket 1 error - " << qs.toStdString() << std::endl; } else { cout << "Socket 1 OK\n"; } return a.exec(); }
I see in console
192.168.3.40
172.16.1.118
127.0.0.1
Socket 1 error - -1It sees the IP - 192.168.3.40 but I get an error. Why?
-
-
@Christian-Ehrlicher said in Problem to bind a UDP socket.:
@jenya7 said in Problem to bind a UDP socket.:
"192.68.3.40"
Typo?
yes. it's "192.168.3.40"
-
So does it work now? If so please print out the errorString.
-
@Christian-Ehrlicher said in Problem to bind a UDP socket.:
So does it work now? If so please print out the errorString.
to print it I added another argument
soc_err = udp_1.Start("192.168.3.40", 8001, &err_str); if (soc_err) { qs = QString::number(soc_err); cout << "Socket 1 error - " << qs.toStdString() << std::endl; cout << err_str.toStdString(); } else { cout << "Socket 1 OK\n"; }
It prints
192.168.3.40
172.16.1.118
127.0.0.1
Socket 1 error - -1
Unknown errorthis way
result = socket->bind(QHostAddress::Any, local_port);
I also get
Socket 1 error - -1
Unknown error -
@jenya7 said in Problem to bind a UDP socket.:
172.16.1.118
127.0.0.1Where do those two IPs come from? Please post your actual code and the real output. Also make sure the IPs you're using are correct (= your host has those IPs assigned)
-
@Christian-Ehrlicher said in Problem to bind a UDP socket.:
@jenya7 said in Problem to bind a UDP socket.:
172.16.1.118
127.0.0.1Where do those two IPs come from? Please post your actual code and the real output. Also make sure the IPs you're using are correct (= your host has those IPs assigned)
172.16.1.118 - is another network card. 127.0.0.1 - default local host.
I changed for PC with only one network card.
cmd->ipconfig
Ethernet adapter Local Area Connection 2:Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::7d2a:547e:e90a:6e3e%12
IPv4 Address. . . . . . . . . . . : 172.16.1.139
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 172.16.1.1soc_err = udp_1.Start("172.16.1.139", 8001, &err_str);
I get
172.16.1.139
127.0.0.1
Socket 1 error - -1
Unknown error -
@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.
-
@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.