Unsolved QUdpSocket->state() is always QAbstractSocket::ConnectedState
-
Hi there!
I have a piece of code that trying to connect some device via UDP.QUdpSocket* udp = new QUdpSocket(); udp->connectToHost(IP, PORT, QIODeviceBase::ReadWrite);
It doesn't matter device is present or not in LAN and switched it on or not, the result of
if (udp->state() == QAbstractSocket::ConnectedState)
is always true.
Are there other methods to obtain connection state more precisely?
(Qt 6.1.1 with Clang based on MinGW 6.1.1 x64, Windows 10 Pro x64)
-
@alxdef said in QUdpSocket->state() is always QAbstractSocket::ConnectedState:
QAbstractSocket::ConnectedState
You forgot about socket bind
-
@qtprogrammer123, bind() is for servers. I tried to use it but the result is same. And my code is more client than server.
-
@alxdef
It does indeed not sound right if it always reports "connected" and you are sure it cannot be!Maybe this is a Qt 6 issue. I might also try a rubbish IP/PORT to see if that still reports connection.
Can only suggest you slot onto QAbstractSocket::stateChanged(QAbstractSocket::SocketState socketState) to report what states it claims to go through, and see if that reveals anything. If you haven't already, you should also catch QAbstractSocket::errorOccurred(QAbstractSocket::SocketError socketError) to see if any error occurs.
-
Just try add udp->bind(QHostAddress::LocalHost, 1234) before connection
-
ouuu if host close connection, you dont see that. Sory its early ;] You should catch errorOccurred signal, it contains informations about host problems
-
I don't see what's wrong here. QUdpSocket is stateless so as soon as the socket is opened it's connected - what else should it report?
-
@Christian-Ehrlicher connectToHost set state - connected, udp is stateless protocol, state is parent method.
Hmm it can be set manualy so try, i write in notepad, so some errors may occure :D
connect(udp, &QUDPSocket::errorOccurred, onErr); void onErr(){ udp.setSocketState(QAbstractSocket::UnconnectedState); }
-
So I decided for me such algorithm:
- No way to check connection state because it's useless.
- Try to work with device right after connectToHost and decide what to do next - set self error state or continue my job.