Results 1 to 2 of 2

Thread: QTcpSocket waiting for write to be read

  1. #1
    Join Date
    Nov 2008
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTcpSocket waiting for write to be read

    Hi there. I have a Qt based service which essentially contains this

    Qt Code:
    1. s = server.nextPendingConnection();
    2. // ..snip.. (read a command string from s)
    3. s->write(some_string);
    4. s->waitForBytesWritten(-1);
    5. delete s;
    To copy to clipboard, switch view to plain text mode 

    A PHP web page connects to it with code like this

    php Code:
    1. $socket = socket_create ( ..snip.. );
    2. socket_connect ($socket, $address, $port);
    3. $command = some_stuff;
    4. socket_write ($socket, $command, strlen($command));
    5. $p = socket_read ($socket, 100, PHP_NORMAL_READ);
    6. socket_close ($socket);
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by jpn; 23rd December 2008 at 14:23. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Boston, MA
    Posts
    40
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket waiting for write to be read

    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.

  3. The following user says thank you to fanat9 for this useful post:

    spraff (24th December 2008)

Similar Threads

  1. Unable to read from QTcpSocket
    By jimroos in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2007, 21:09
  2. How to read more bytes using QTcpSocket?
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 20:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.