Results 1 to 6 of 6

Thread: QTcpSocket

  1. #1

    Default QTcpSocket

    I have a problem with my TCPSocket. I wan't to send two strings using write.
    This is the send code:

    Qt Code:
    1. tcpClient.write(("derrors;"+item->text(0)).toAscii().data());
    2. tcpClient.write(("werrors;"+item->text(0)+";;true;").toAscii().data());
    To copy to clipboard, switch view to plain text mode 

    This needs to send 2 pieces:
    "derrors;00001"
    and
    "werrors;00001;;true;"

    But in fact it sends this: "derrors;00001werrors;00001;;true;"
    Even if there is a delay between them.
    How can I split this up to two parts?

  2. #2
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket

    Actually i don't know this things.I think that it is sending separately but when you are receiving you read all bytes at a time.So you should add some header and accordingly you extract it in receiver side.

  3. #3

    Default Re: QTcpSocket

    No, that's not the problem.
    I scanned the outgoing traffic on my computer and find out it is really transmittings it in one block.

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket

    In the TCP protocol the exchanged data is a stream of bytes. There is absolutely no guarantee that the received data will be split into the same chunks as the sent data. If you want to preserve such a structure you need to implement your own solution as you would do if you were writing to and then reading from a file. There are two common ways to achieve this:
    1. use a special delimiter sequence (e.g. newline, NUL character), but make sure you escape it were it to occur in the payload,
    2. send the size of the chunk, then the chunk itself.

    In your case solution 1 seems most appropriate, since you appear to be using a text-based protocol.

  5. #5
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket

    You're right.

    QAbstractSocket uses a write buffer to reduce network overhead.
    To obtain 2 single TCP packets use QAbstractSocket::flush()
    Qt Code:
    1. tcpClient.write(("derrors;"+item->text(0)).toAscii().data());
    2. tcpClient.flush();
    3. tcpClient.write(("werrors;"+item->text(0)+";;true;").toAscii().data());
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  6. #6
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket

    @mcosta

    I would not rely on that for several reasons. First, the doc for QAbstractSocket::flush() specifies that it "writes as much as possible from the internal write buffer to the underlying network socket, without blocking"; therefore it would be necessary to wait until all the data be sent before writing the rest. Next, even if you ensured that any pending data be sent before each write(), each such call could generate several packets. Finally, the packets are translated back into a stream on the receiving end, and there is no exact correspondence between packets and data chunks actually read from the socket.

    Therefore I would recommend either to implement one of the solutions I mentioned or to choose another protocol than TCP.

Similar Threads

  1. QTcpSocket
    By Fallen_ in forum Qt Programming
    Replies: 5
    Last Post: 26th August 2010, 05:32
  2. Need Help with QTcpSocket
    By jknotzke in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2009, 13:55
  3. QTcpSocket
    By pdoria in forum Qt Programming
    Replies: 1
    Last Post: 16th July 2009, 17:52
  4. QTcpSocket and Qt 4.2.3
    By slcotter in forum Qt Programming
    Replies: 4
    Last Post: 9th May 2007, 16:14
  5. QT4.2.2 and QTcpSocket
    By Nyphel in forum Qt Programming
    Replies: 13
    Last Post: 11th March 2007, 11:30

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.