Results 1 to 4 of 4

Thread: QTcpSocket write():: How many data sends?

  1. #1
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QTcpSocket write():: How many data sends?

    Hi,
    I have a network application, with a client-server design.

    Server read a text file and send this information to client.

    This is a little extract of my code:

    Qt code:
    Qt Code:
    1. QString xTmpString;
    2. for (int i = 0; i < xDataStringList.size(); i++)
    3. {
    4. xTmpString = xDataStringList.at(i);
    5. pxTransmissionSocket->write(xTmpString.toLatin1());
    6. }
    To copy to clipboard, switch view to plain text mode 

    This code work correctly but I have a question:

    I can see that client receive only one message whit all the text.
    Suppose that I have 50 String. I want that QTcpSocket execute 50 write and the client recevice 50 message?
    How I can do it?

    Can anyone help me?

    Bye

  2. #2
    Join Date
    Apr 2010
    Posts
    34
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket write():: How many data sends?

    Obviously the problem is, QTcpSocket works asynchronously, so calling "write" just puts your data to a buffer but sending occurs independently. Since the copying data to a buffer in your code is certainly faster, you fill the buffer with all data earlier than the data are sent.
    Put "waitForBytesWritten" call after the write command if you need to wait with the next write step until the data are sent.

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

    Daxos (29th July 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTcpSocket write():: How many data sends?

    Quote Originally Posted by Vit Stepanek View Post
    Put "waitForBytesWritten" call after the write command if you need to wait with the next write step until the data are sent.
    Note that this blocks and it should only be used when no event loop is available (like in a thread with no event loop)

    Just send 50 messages and read them at the other side. How hard can this be, just make sure you can seperate them in some way.
    Use a QXmlStreamReader and Writer, then you don't have to care about headers or control characters to split the messages.

  5. The following user says thank you to tbscope for this useful post:

    Daxos (29th July 2010)

  6. #4
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket write():: How many data sends?

    Quote Originally Posted by Vit Stepanek View Post
    Obviously the problem is, QTcpSocket works asynchronously, so calling "write" just puts your data to a buffer but sending occurs independently. Since the copying data to a buffer in your code is certainly faster, you fill the buffer with all data earlier than the data are sent.
    Put "waitForBytesWritten" call after the write command if you need to wait with the next write step until the data are sent.
    Ok thanks, now my program working correct.

    Note that this blocks and it should only be used when no event loop is available (like in a thread with no event loop)

    Just send 50 messages and read them at the other side. How hard can this be, just make sure you can seperate them in some way.
    Use a QXmlStreamReader and Writer, then you don't have to care about headers or control characters to split the messages.
    There isn't a problem because I don't have Gui application.

    Now I have another problem:
    Memory use by client is constantly increase and I have a memory overflow.

    I have try with flush instruction but I don't have solve this problem.

    QtcpSocket store some information?


    This is my client code:
    Qt code
    [code]
    QString xInfo
    xInfo = pxSocket->readAll();
    [code]

    I try to use a QByteArray but this code don't work proprerly:
    (Server code)
    Qt Code:
    1. 1 for (int i = 0; i < xDataStringList.size(); i++)
    2. 2 {
    3. 3 QByteArray xBlock;
    4. 4 QDataStream xOut(&xBlock, QIODevice::WriteOnly);
    5. 5 xOut.setVersion(QDataStream::Qt_4_0);
    6. 6 xOut << (quint16)0;
    7. 7 xOut << xDataStringList.at(i);
    8. 8 xOut.device()->seek(0);
    9. 9 xOut << (quint16)(xBlock.size() - sizeof(quint16));
    10. 10 pxTransmissionSocket->write(xBlock);
    11. }
    To copy to clipboard, switch view to plain text mode 

    xBlock at line 10 is empty.

    Thanks, bye

Similar Threads

  1. QTcpSocket write() Trasmission Data Design
    By Daxos in forum Qt Programming
    Replies: 4
    Last Post: 15th July 2010, 19:19
  2. QTcpSocket Write & \0 \x00
    By NewGuy5800 in forum Newbie
    Replies: 1
    Last Post: 25th April 2010, 11:23
  3. QTcpSocket sends data twice with flush()
    By xenome in forum Qt Programming
    Replies: 11
    Last Post: 2nd April 2010, 09:12
  4. read and write on qtcpsocket
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 10:42
  5. QTcpSocket waiting for write to be read
    By spraff in forum Qt Programming
    Replies: 1
    Last Post: 23rd December 2008, 20: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.