Results 1 to 4 of 4

Thread: How to write to a TCPSocket with a QTextStream?

  1. #1
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to write to a TCPSocket with a QTextStream?

    Hi,
    I can't figure out why it doesn't send my data over the socket.
    This is my code.

    buffer is a class which contains a Qlist with myStructs in it.
    This is because two threads are working with the buffer.
    One adds the other gets.

    Qt Code:
    1. void MyClass::WriteData{
    2. myStruct package;
    3.  
    4. while(!buffer->isEmptyMyStruct()){
    5. msleep(20);
    6. package = buffer->getMyStruct();
    7.  
    8. QByteArray block;
    9. QTextStream out(&block, QIODevice::ReadWrite);
    10.  
    11. out << package;
    12. out.flush();
    13.  
    14. tcpSocket->write(block);
    15. tcpSocket->flush();
    16.  
    17. emit message(package.message);
    18. emit message(package.id);
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: How to write to a TCPSocket with a QTextStream?

    To write binary data use QDataStream instead QtextStream.
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to write to a TCPSocket with a QTextStream?

    Quote Originally Posted by mcosta View Post
    To write binary data use QDataStream instead QtextStream.
    I'm not writing the data as binary but as text.
    I figured out that it's a lot easier to just write to the socket directly. This solved my problem

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to write to a TCPSocket with a QTextStream?

    What does write() return?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 4
    Last Post: 8th January 2008, 19:41
  2. FSWriteFork in MAC OS to write data to a file.
    By vishal.chauhan in forum General Programming
    Replies: 5
    Last Post: 2nd July 2007, 07:48
  3. Create QTextStream
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 17th June 2007, 21:25
  4. QTextStream capture stdout from xsltParseStylesheetFile
    By patrik08 in forum Qt Programming
    Replies: 9
    Last Post: 25th June 2006, 12:24
  5. segfault on qtextstream
    By patcito in forum Qt Programming
    Replies: 13
    Last Post: 26th February 2006, 14:10

Tags for this Thread

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.