Results 1 to 4 of 4

Thread: QTcpSocket question, write method

  1. #1
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QTcpSocket question, write method

    Hello,
    I have a problem with QTcpSocket's "write" method.
    I have a client and a server. Server tries to send some data to a client using the write function, it returns a correct number of bytes sent. But in reality, data is not sent, client can't read it. I tried to catch messages sent by server using WireShark and discovered that message is not sent, even though the write method does not return -1, it returns a correct number.

    What could be a source of that error?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTcpSocket question, write method

    You are probably not running a Qt event loop but since we cannot see your code we can only guess. QIODevice::write() returns the number of bytes written to the device. When the QIODevice is a socket the bytes will be sent over the wire at some time in the future by the asynchronous processing of the Qt event loop. If your code does not return to the Qt event loop the data is not sent.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

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

    Anslem (1st June 2014)

  4. #3
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket question, write method

    ChrisW6
    Sorry for not posting a code before, it was a little bit complex. Anyway, your answer helped me. Here is something like the code i have now:
    Qt Code:
    1. clientConnectionSocket->waitForReadyRead();
    2. QByteArray data = clientConnectionSocket->readAll();
    3. clientConnectionSocket->write("some data");
    4. QTest::qWait(1000);
    To copy to clipboard, switch view to plain text mode 

    I added qWait to manually start event loop. Is it a bad solution?
    Last edited by Anslem; 19th May 2014 at 09:56.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTcpSocket question, write method

    QTest is for testing so, yes, putting that in production code is not a good thing.
    Qt networking works best when used through it asynchronous (normal, non-blocking) interface.
    Qt network programming
    Network examples especially the non-blocking fortune client.

  6. The following user says thank you to ChrisW67 for this useful post:

    Anslem (1st June 2014)

Similar Threads

  1. QTcpSocket: crash on write
    By valerianst in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2014, 15:28
  2. QTcpSocket write():: How many data sends?
    By Daxos in forum Qt Programming
    Replies: 3
    Last Post: 29th July 2010, 10:27
  3. QTcpSocket Write & \0 \x00
    By NewGuy5800 in forum Newbie
    Replies: 1
    Last Post: 25th April 2010, 10:23
  4. read and write on qtcpsocket
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 09:42
  5. QTcpSocket waiting for write to be read
    By spraff in forum Qt Programming
    Replies: 1
    Last Post: 23rd December 2008, 19:12

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.