Simple TCP Server failure.
-
I've already seen fortune server and client example, but it uses QDataStream for transfer data, which I'm not using. Imagine instead of client_send, I'm using custom QTcpSocket::write method (which modifies first 8 bytes to size of data to be sent and then actual data).
However, output must be like this for each client_send function, but I really don't get how to make this happen.
[code]hello
world
this
is
test[/code]EDIT: I have solved it by modifying this piece of code, but I'm interested if this way is safe.
[code]if(!this->blockSize)
{
qDebug() << this->chunkArray;
//emit dataReady(this->chunkArray);
this->chunkArray.clear();
if(this->bytesAvailable())
emit readyRead(); // <-- is this way safe?
}[/code]Regards.
-
I am using QTcpClient without QDataStream and without problems.
However, your sending application does not have an event loop as both the fortune server and fortune client have. AFAIK that is the reason that your application does not send all the stuff (assuming that your sending app is also using QTcpClient). -
Just saw that you have edited your last post.
Your receiving part might receive the incoming data byte by byte. Typically your application may be triggered by a readyRead signal and able to read all information, but all has been introduced to the buffer yet. -
[quote author="Overflowz" date="1371142820"]You mean it could read same data twice or more and append to buffer which will cause false results?[/quote]
My assumption that client_readyRead is triggered by QTcpSocket signal readyRead, so it might be triggered several times.Nevertheless, you did not disclose yet, what kind of application you are using for sending. If this is using QTcpSocket communication, I am wondering if this can work, but may be. Or there is much more around than you show. At the main.cpp does not set up an event loop.
In case QTcpSocket is used for sending and when this is possible without event loop, you might exit the program before all stuff has been send.Again my suggestion would be to strip down the fortune examples to what you need. This is probably the easier start for you to work the issues out.
-
QTcpSocket is non-blocking.
However, are you sure that the stuff is being sent by your application before it closes? I might well be that the application is writing to buffers and it closes before the buffers have been emptied. If that is the case, it would completely explain the issues you have. -
as I said in previous post, I fixed it by modifying that piece of code which I posted above. I'm 100% sure that data is sent successfully, because of bytesAvailable() returns count of whole data length (I'll add error checking too, but for my debugging it's not important, I'm checking it from debugger).
Regards.