Results 1 to 5 of 5

Thread: qt socket question

  1. #1
    Join Date
    Aug 2006
    Posts
    14
    Thanks
    4
    Qt products
    Qt4

    Default qt socket question

    I am still very new to C++/Qt programming. I was hoping that someone could shed some light on something for me:

    Qt Code:
    1. void Thread::run()
    2. {
    3. //initalize socket, start connection and detect data available
    4. socket = new QTcpSocket;
    5.  
    6. if (socket->setSocketDescriptor(sock))
    7. {
    8. QString welcomeMsg = QString("Welcome to the middleMan\n\n");
    9. QByteArray block;
    10. QDataStream out(&block, QIODevice::WriteOnly);
    11. out.setVersion(QDataStream::Qt_4_0);
    12. out << (quint16)0;
    13. out << welcomeMsg;
    14. out.device()->seek(0);
    15. out << (quint16)(block.size() - sizeof(quint16));
    16.  
    17. socket->write(block);
    18. }
    19.  
    20. while (socket->state() == QAbstractSocket::ConnectedState)
    21. {
    22.  
    23. if (socket->bytesAvailable() < 1)
    24. {
    25. qDebug("Waiting for data...");
    26.  
    27. if (!socket->waitForReadyRead())
    28. break;
    29. }
    30.  
    31. // while (socket->bytesAvailable() > 0)
    32. // {
    33. if (socket->bytesAvailable() > 0)
    34. {
    35. qDebug("Data available");
    36.  
    37. QByteArray incomingBlock = socket->readAll();
    38.  
    39. if (incomingBlock.contains("xml") && !incomingBlock.isEmpty())
    40. {
    41. dataHandler = new xmlDataHandler;
    42.  
    43. dataHandler->doParse( &incomingBlock );
    44. }
    45.  
    46. /*
    47.   char c;
    48.  
    49.   socket->getChar(&c);
    50.  
    51.   printf("%c", c);
    52.   */
    53. }
    54. // }
    55. }
    56.  
    57. qDebug("Connection closed");
    58. }
    To copy to clipboard, switch view to plain text mode 

    To explain a little bit. This is the run code for a thread that is created and started each time a new connection is requested. I am trying to stream short bits of dynamically created xml like data to the server from a client. I basically want to know when the end of a stream has been reached. How do you figure out the data that is coming across the socket. I assume the beginning of the stream is the header information. Then the data is passed. How do I separate those and only save the data and close the connection after the data is finished streaming? Thanks in advance. Even pointing to a good sockets tutorial would suffice.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qt socket question

    Quote Originally Posted by bluesguy82
    out << (quint16)(block.size() - sizeof(quint16));
    You don't need this, since QDataStream sends string length too.

    http://doc.trolltech.com/4.1/datastreamformat.html

    Quote Originally Posted by bluesguy82
    I basically want to know when the end of a stream has been reached.
    You will never know, unless the other side sends some kind of EOT message. Since you use XML, all you need to know is whether all tags have been closed or not.

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

    bluesguy82 (29th August 2006)

  4. #3
    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: qt socket question

    There is nothing like "the end of the stream" when talking about TCP sockets. An end of the stream is determined by closing the connection on the transmitter side (for example by using shutdown() function from the BSD socket API).

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

    bluesguy82 (29th August 2006)

  6. #4
    Join Date
    Aug 2006
    Posts
    14
    Thanks
    4
    Qt products
    Qt4

    Default Re: qt socket question

    on the other piece though, how do i decide when the stream starts to flow what is message header info and what is data?

  7. #5
    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: qt socket question

    If you are using some kind of protocol (for example HTTP), the protocol should clearly state where does data begin or end. If you implement your own protocol, provide your own means to determine that. If you're using HTTP, you might find using QHttp instead of lower level socket classes convenient.

Similar Threads

  1. Can any One help me? - Socket Programming?
    By vinod in forum Qt Programming
    Replies: 6
    Last Post: 18th November 2009, 09:46
  2. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 14:38
  3. Basic question on new and delete
    By jcr in forum General Programming
    Replies: 25
    Last Post: 14th February 2006, 15:09
  4. QTextEdit Qt4: simple question
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2006, 12:03
  5. xml with binary question
    By TheKedge in forum Qt Programming
    Replies: 7
    Last Post: 12th January 2006, 23:21

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.