PDA

View Full Version : QTcpSocket write():: How many data sends?



Daxos
28th July 2010, 08:24
Hi,
I have a network application, with a client-server design.

Server read a text file and send this information to client.

This is a little extract of my code:

Qt code:



QString xTmpString;
for (int i = 0; i < xDataStringList.size(); i++)
{
xTmpString = xDataStringList.at(i);
pxTransmissionSocket->write(xTmpString.toLatin1());
}


This code work correctly but I have a question:

I can see that client receive only one message whit all the text.
Suppose that I have 50 String. I want that QTcpSocket execute 50 write and the client recevice 50 message?
How I can do it?

Can anyone help me?

Bye

Vit Stepanek
28th July 2010, 09:56
Obviously the problem is, QTcpSocket works asynchronously, so calling "write" just puts your data to a buffer but sending occurs independently. Since the copying data to a buffer in your code is certainly faster, you fill the buffer with all data earlier than the data are sent.
Put "waitForBytesWritten" call after the write command if you need to wait with the next write step until the data are sent.

tbscope
28th July 2010, 11:06
Put "waitForBytesWritten" call after the write command if you need to wait with the next write step until the data are sent.

Note that this blocks and it should only be used when no event loop is available (like in a thread with no event loop)

Just send 50 messages and read them at the other side. How hard can this be, just make sure you can seperate them in some way.
Use a QXmlStreamReader and Writer, then you don't have to care about headers or control characters to split the messages.

Daxos
29th July 2010, 10:27
Obviously the problem is, QTcpSocket works asynchronously, so calling "write" just puts your data to a buffer but sending occurs independently. Since the copying data to a buffer in your code is certainly faster, you fill the buffer with all data earlier than the data are sent.
Put "waitForBytesWritten" call after the write command if you need to wait with the next write step until the data are sent.

Ok thanks, now my program working correct.



Note that this blocks and it should only be used when no event loop is available (like in a thread with no event loop)

Just send 50 messages and read them at the other side. How hard can this be, just make sure you can seperate them in some way.
Use a QXmlStreamReader and Writer, then you don't have to care about headers or control characters to split the messages.


There isn't a problem because I don't have Gui application.

Now I have another problem:
Memory use by client is constantly increase and I have a memory overflow.

I have try with flush instruction but I don't have solve this problem.

QtcpSocket store some information?


This is my client code:
Qt code


QString xInfo
xInfo = pxSocket->readAll();
[code]

I try to use a QByteArray but this code don't work proprerly:
(Server code)
[code]
1 for (int i = 0; i < xDataStringList.size(); i++)
2 {
3 QByteArray xBlock;
4 QDataStream xOut(&xBlock, QIODevice::WriteOnly);
5 xOut.setVersion(QDataStream::Qt_4_0);
6 xOut << (quint16)0;
7 xOut << xDataStringList.at(i);
8 xOut.device()->seek(0);
9 xOut << (quint16)(xBlock.size() - sizeof(quint16));
10 pxTransmissionSocket->write(xBlock);
}


xBlock at line 10 is empty.

Thanks, bye