Unsolved Receiving data from QTcpSocket
-
I have written a class derived from QTcpSocket, I connect to a server:
connectToHost("localhost", 8123); if ( waitForConnected(20000) != true ) { emit terminateModule(); } //Associate this TCP socket with the output data stream mdsOut.setDevice(this); mdsOut.setVersion(QDataStream::Qt_5_14); //Build the module start-up message QJsonObject objMsg; objMsg.insert(clsJSON::mscszModule, clsModHelper::msszTitle); objMsg.insert(clsJSON::mscszMsgType, clsJSON::mscszCmdInitial); objMsg.insert(clsJSON::mscszPort, clsModHelper::muint16ModulePort); mdsOut << QJsonDocument(objMsg).toJson(QJsonDocument::Compact);
Using the debugger I can see the data is sent and on the application that will receive the data, I have a class derived from QTcpServer in its constructor:
listen(QHostAddress::Any, 8123);
I implement the incomingConnection slot:
void clsSocketServer::incomingConnection(qintptr sfd) { qDebug() << "incomingConnection"; clsSocketClient* pSckClient = new clsSocketClient(this); pSckClient->setSocketDescriptor(sfd); connect(pSckClient, &QTcpSocket::disconnected, pSckClient, &clsSocketClient::onDisconnected); connect(pSckClient, &QTcpSocket::readyRead, pSckClient, &clsSocketClient::onReadyClient); }
And I can see this is getting called, but there is no activity in the onReadyClient slot:
void clsSocketClient::onReadyClient() { qDebug() << "onReadyClient"; }
I must be missing something very fundamental ?
-
@SPlatten "Note: If another socket is created in the reimplementation of this method, it needs to be added to the Pending Connections mechanism by calling addPendingConnection()."
https://doc.qt.io/qt-5/qtcpserver.html#incomingConnection -
@jsulm , I've added this and rebuilt, no change, I think I'm missing the incoming data stream which I'm looking at now.
-
What is mdsOut and are you sure you're sending the data out (e.g. by calling flush() - and check it's return value)
-
@Christian-Ehrlicher , I haven't called flush...I haven't seen that in any samples I've seen either. I'll give it a try.
mdsOut is an instance of QDataStream.
-
@Christian-Ehrlicher , I added called to flush after sending, no difference.
-
@SPlatten said in Receiving data from QTcpSocket:
I added called to flush after sending, no difference.
And is the signal
bytesWritten(qint64)
emitted by the socket?
You can also try to remove TCP delay on client tcp socket withsocket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
-
@KroMignon , thank you, I've added:
connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));
To the constructor of my class which is derived from QTcpSocket. The slot:
void clsModHelper::onBytesWritten(qint64 int64Written) { qDebug() << "clsModHelper::onBytesWritten:" << int64Written; }
I'm not seeing anything when the data is written. The slot isn't getting called at all.
-
Maybe the data is already received before you connect the signal.
How about printpSckClient->bytesAvailable()
inincomingConnection
? -
@Bonnie , added that:
void clsSocketServer::incomingConnection(qintptr sfd) { qDebug() << "incomingConnection"; clsSocketClient* pSckClient = new clsSocketClient(sfd, this); addPendingConnection(pSckClient); qDebug() << pSckClient->bytesAvailable(); }
I see:
D00000000000000000020:incomingConnection D00000000000000000021:0
-
@SPlatten said in Receiving data from QTcpSocket:
I'm not seeing anything when the data is written. The slot isn't getting called at all.
I am a little lost.
Just to be sure a understand what you have done:- the TCP accept did work
- you have create a new tcp socket to handle connection with client called
pSckClient
- you have send data on
pSckClient
to the TCP client - your TCP client does not receive anything.
Is this correct?
-
@KroMignon , The incomingConnection is getting called, in this slot I create a new client object using the descriptor parameter passed to the incomingConnection slot.
Data is sent from by the client application but I'm not seeing anything on the receiving application which is listening to localhost and port 8123.
-
@SPlatten
Wait, you are sayingbytesWritten
is never emitted in the sending side (the client)?
Then we should start from the sending part... -
@SPlatten said in Receiving data from QTcpSocket:
The incomingConnection is getting called, in this slot I create a new client object using the descriptor parameter passed to the incomingConnection slot.
Data is sent from by the client application but I'm not seeing anything on the receiving application which is listening to localhost and port 8123.Okay, let's continue step by step:
- what is the return value of
socket->write()
from the client application? Do you really write something on TCP socket? - is
bytesWritten(qint64)
emitted on client side? - are you sure the event loop of the tread used by the socket is not locked? For example on server side
waitForConnected()
locks the event loop. The event loop have to be called to emit thebytesWritten(qint64)
signal
Same on server side:
- are you sure the event loop used by the connection socket in not locked
- what is the return value of
-
@KroMignon , I'm not calling socket->write(), I'm using QDataStream where I have an instance call mdsOut and sending data to that:
mdsOut << QJsonDocument(objMsg).toJson(QJsonDocument::Compact);
I'm not seeing anything written from the onBytesWritten slot. It does appear that the waitForConnection is blocking and never exits.
-
@Bonnie , I'm happy to try anything, apart from the initial connection, no data seems to be getting sent.
-
@SPlatten said in Receiving data from QTcpSocket:
I'm not calling socket->write(), I'm using QDataStream where I have an instance call mdsOut and sending data to that:
I would suggest you to change this to:
QByteArray block; QDataStream mdsOut(&block, QIODevice::WriteOnly); mdsOut.setVersion(QDataStream::Qt_5_14); //Build the module start-up message QJsonObject objMsg; objMsg.insert(clsJSON::mscszModule, clsModHelper::msszTitle); objMsg.insert(clsJSON::mscszMsgType, clsJSON::mscszCmdInitial); objMsg.insert(clsJSON::mscszPort, clsModHelper::muint16ModulePort); mdsOut << QJsonDocument(objMsg).toJson(QJsonDocument::Compact); write(block);
-
@KroMignon said in Receiving data from QTcpSocket:
QByteArray block;
Whats wrong with the way I did it? Thats what all the examples I've seen are doing?
-
@SPlatten said in Receiving data from QTcpSocket:
Whats wrong with the way I did it? Thats what all the examples I've seen are doing?
I don't know if there is something wrong, but this way you can check what and when something is written on TCP socket.
Which is what we want to achieve ==> be sure TCP client is sending something to server. -
@KroMignon , right now it appears that the application is never getting past waitForConnected, even with a 20 second timeout.