PDA

View Full Version : QTcpSocket waiting for write to be read



spraff
23rd December 2008, 15:21
Hi there. I have a Qt based service which essentially contains this


s = server.nextPendingConnection();
// ..snip.. (read a command string from s)
s->write(some_string);
s->waitForBytesWritten(-1);
delete s;

A PHP web page connects to it with code like this

$socket = socket_create ( ..snip.. );
socket_connect ($socket, $address, $port);
$command = some_stuff;
socket_write ($socket, $command, strlen($command));
$p = socket_read ($socket, 100, PHP_NORMAL_READ);
socket_close ($socket);

The Qt service reads the command string fine, but the PHP errors on socket_read saying "connection reset by peer". I guess this means Qt deletes the socket before the read is finished. I could make the PHP write an "I'm finished" string, but that doesn't alter the fundamental problem. How do I make Qt not close the socket until socket_read has finished?

Thanks.

fanat9
23rd December 2008, 20:12
In Qt part you must leave socket opened until remote part close it.

Try waitForDisconnected() with some huge timeout (default value 30sec - more then enought) before deleting/closing your socket.