PDA

View Full Version : [QTcpServer] read from socket returned by nextPendingConnection



saknopper
22nd June 2007, 16:36
Hi all,

I'm having some trouble to read from a QTcpSocket returned by QTcpServer. The situation is like this:

1) Server listens
2) Clients connects and sends a message to identify itself
3) Server creates a Client object using the data in this message

The problem is #3, a QTcpSocket is returned when calling nextPendingConnection() and the message from the client has been sent correctly. Though the bytesAvailable() function on the returned QTcpSocket keeps returning 0. With as a result that I can't read the message obviously and thus can't create the client object.

The weird thing is, that when I immediately create a thread to handle the newly returned QTcpSocket I can read from the socket just fine. But since the client object is a thread itself it seems pretty bad to create another thread just to handle the message so I can extract the data.

Is there anyone who has encountered this before and possibly knows a workaround? I'm willing to post a testcase which probably will illustrate the problem better.

jacek
22nd June 2007, 23:03
Though the bytesAvailable() function on the returned QTcpSocket keeps returning 0.
Maybe the data hasn't arrived yet? You can use QAbstractSocket::waitForReadyRead() or make the client object process that identification data.

saknopper
23rd June 2007, 09:13
Processing the message in the client object itself is not really an option at the moment. Since to create such an object I need the data from the message.

Anyway, if I use waitForReadyRead() it does just nothing, the bytesAvailable() method is called with a little timeout in between of course. It keeps returning 0.

I've tested it with telnet as well, the connection itself is established properly. bytesAvailable() returns 0 which is ok, since I haven't sent anything. But when I do, it still keeps returning 0. I -can- however write to the socket!

Like I said, when I instantly create a new thread handling the new QTcpSocket I can read just fine.