[Qt-interest] QTcpSocket weirdness

Anatoly Burakov burakov.anatoly at googlemail.com
Thu Apr 1 05:20:19 CEST 2010


Hi all

I wrote about this before but still can not find the exact cause of such
behaviour.

My application uses QtNetwork and QTcpServer/QTcpSocket in order to
communicate. The server and client use a custom connection protocol
(made of integers and objects). The server side is structured as a lot
of nested switch/case statements (that is, i can't imagine use of QT
signal/slot mechanism in such situation) and resides on a separate
thread (nextPendingConnection etc). The whole idea looks something like
this:

------ server side

// wait for connection
// connected, wait for incoming integer (while bytesAvailable < sizeof(int))
// based on incoming data use switch/case to determine action and send
another integer, wait until it's written (while bytesToWrite > 0) and
wait for reply

And there is lots of nesting and lots of actions (right now i have
somewhere around 40 integer signals defined in enums).

The client side is relatively straight-forward and uses separate
functions to ask server about different things.

Now, the problem is when i try to communicate - it seems to work very
unstably, sometimes working, sometimes not - sometimes going the whole
way, sometimes breaking around 5th nesting level, sometimes breaking
around 2nd etc. I can't determine the reason behind this behaviour, most
of the time communication just times out (data is sent on one side but
not recieved by the other), sometimes it gives "Unknown Error" (returned
by QTcpSocket.errorString()).

At first someone on the mailing list pointed out that it might be due to
QT not being able to process events in timely manner, and suggested
using signals. Unfortunately, this is not an option, so i tried it the
other way - put in qApp->processEvents() around every piece of code
remotely connected to sending and recieving data through sockets. I even
tried putting a 1000ms timeout for events (qApp->processEvents(...,
1000)) but it didn't help. From what i have described it doesn't look
like the problem is related to QT's events - there is something wrong
with sockets (as i said earlier, most of the time the data never arrives
to the destination socket).

So far i can think of only one more thing to try - to put everything
into byte arrays and read/write directly to the sockets using
QTcpSocket.read()/write() functions (as opposed to using streams). Any
other ideas? And what is that "Unknown error" stands for?

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5238 bytes
Desc: S/MIME Cryptographic Signature
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100401/7904939e/attachment.bin 


More information about the Qt-interest-old mailing list