QUdpSocket disconnects after data writing
-
Hello everyone! I'm trying to write a client for server (which is not mine) using QUdpSocket. But I have some problems. The main one is that socket goes to unconnected state whenever after any data was written to it.
Init socket example:
mUdpSocket = new QUdpSocket(this) connect(mUdpSocket, &QUdpSocket::readyRead, this, &SessionManager::onSocketReadyRead); connect(mUdpSocket, &QUdpSocket::bytesWritten, [](quint64 b) { qDebug() << "____ UDP BYTES WRITTEN:" << b; }); connect(mUdpSocket, &QUdpSocket::stateChanged, [this](QAbstractSocket::SocketState socketState) { qDebug() << "____ UDP SOCKET STATE CHANGED:" << socketState; }); qDebug() << "____ BIND" << mUdpSocket->bind(QHostAddress::Any);
Write example:
void SessionManager::sendMessage(const QByteArray& message) { if (!message.isEmpty()) { mUdpSocket->writeDatagram(message, QHostAddress(mHost), G_SESSION_PORT); // TODO: check written != -1 } }
Problem occurs after calling
writeDatagram
. I can see that 76 bytes was written succesfully and was sent to server: signalbytesWritten
reports the same amount of bytes written as insendMessage
method. I also can see with Wireshark that package was sent successfully to the server. After that socket goes to unconnected state with no error (QAbstractSocket::UnknownSocketError). At that moment I can see that server tries to send me several packages, but noreadyRead
signal was emmited from socket. Can someone help me? I dont know what is going wrong. -
@ClingerWinger the most likely problem is, that your
QUdpSocket
is destroyed - directly or indirectly - afterwriteDatagram()
.Its impossible to guess more from your current code, so if my answer doesnt help you you should upload the complete example so others can try it.
Regardsü
-
Just realised that I forgot what happened at my main.cpp. Object SessionManager was created at lambda, and destroyed there immediately. Sorry for you bother.
-
@ClingerWinger Glad you figured it out and thanks for the feedback :)