PDA

View Full Version : QTcpSocket - try to send bunch of requests



nickla
15th March 2012, 19:50
I am trying to send 2 request one by one at same time. My code is following (this is example code):



QTcpSocket client;
...
client->write(block);
client->write(block);


Problem is following. Server receives only first request. There is no second request. I sniffed using wireshark and see that there is no second request in tcp packets.

What must i do to send many requests via QTcpSocket one by one?

Added after 29 minutes:

I inserted qDebug() << this->bytesAvailable() << "bytes"; to server in readyRead() and qDebug() << this->bytesToWrite(); after each client->write(block); in client. Also, I added this to client:



connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));

void Connection::bytesWritten(qint64 count)
{
qDebug() << count << "bytes written";
}


I send ORDER_STATUS_GET_LIST constant in first request and ORDER_GET_LIST in second. I added data output in server. I received first command.

There is output listing:

Client:


Sending ORDER_STATUS_GET_LIST
11 bytes to write
Sending ORDER_GET_LIST
68 bytes to write
68 bytes written


Server:


68 bytes
ORDER_STATUS_GET_LIST received


Added after 17 minutes:

I found solution myself. I think that somebody needs this too.

Solution is simple:


QTcpSocket client;
...
client->write(block);
client->flush();
client->write(block);
client->flush();


This is happen because QT tcp buffer is not empty when second request is executed.

wysota
15th March 2012, 20:57
What must i do to send many requests via QTcpSocket one by one?
You can't do that with TCP. TCP has no concept of "requests" or "datagrams" or any other kind of records. It is a contignuous flow of octets (bytes). It is up to the application to interpret those octets with the use of some kind of separators (e.g. HTTP uses newlines as separators). Even if you use flush() as in your code, you can't guarantee that the receiving end will get the data in two separate blocks. They can arrive as one block or as 1000 blocks. In your example you get two blocks only because the client and the server are close to each other (in term of network hops) and there is no congestion in the network.