Page 3 of 3 FirstFirst 123
Results 41 to 47 of 47

Thread: QTcpSocket readyRead strange behavior

  1. #41
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket readyRead strange behavior

    what you are saying is that i must not make calls to the QTcpSocket object's methods (that works in the QThread thread) with my main thread.

    Well, that's logical.

    thanks

  2. #42
    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 readyRead strange behavior

    No, the problem is that part of the code that you think that runs in the thread does in fact not run in the thread.

    All the code in the slot uses the main event loop while all the code in the run() function uses the thread event loop.

    There are two ways to solve this:
    1. As suggested, do not use threads.
    2. Use threads correctly.

    The last part is, for example, done by putting all your socket handling code in a separate QObject subclass.
    Then move the object to a new thread. This way you're sure that the code in the slot also uses the thread event loop (because the complete object is moved to the thread).

  3. #43
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket readyRead strange behavior

    Hi,
    You could look at the fortunecookie server and client examples.

    In that example the length of the "packet" is put upfront. On the receiving side, the length is read, and they keep on receiving until all the bytes are received. This is what you want.

    It is best to see a TCP connection as a 'stream' of bytes that are coming in. You have to find the start and end of your 'message' yourself.

    Best regards,
    Marc

  4. #44
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QTcpSocket readyRead strange behavior

    Quote Originally Posted by marcvanriet View Post
    In that example the length of the "packet" is put upfront. On the receiving side, the length is read, and they keep on receiving until all the bytes are received. This is what you want.
    That's exactly what he has, you know
    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.


  5. #45
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket readyRead strange behavior

    Oops... didn't go to the very last page of this thread. Well, better safe then sorry...

  6. #46
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket readyRead strange behavior

    yeah, i think i'll just use threads correctly. I didn't understand well.

  7. #47
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QTcpSocket readyRead strange behavior

    Quote Originally Posted by naroin View Post
    yeah, i think i'll just use threads correctly. I didn't understand well.
    The point we are trying to stress is that using threads doesn't give you any benefit over not using threads and at the same time it gives you many problems (you have already stumbled upon one of them).
    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. QtcpSocket readyRead Problem with Java Client
    By Bernie in forum Qt Programming
    Replies: 6
    Last Post: 12th February 2011, 12:59
  2. QTcpSocket and readyRead and QTimer
    By cafu in forum Qt Programming
    Replies: 2
    Last Post: 18th December 2009, 14:36
  3. Question in readyRead(QTCPSOCKET)
    By morgana in forum Qt Programming
    Replies: 2
    Last Post: 24th July 2008, 19:11
  4. QTcpSocket readyRead and buffer size
    By pdoria in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2008, 11:11
  5. Strange QTcpSocket behavior (debugged with Ethereal)
    By mdecandia in forum Qt Programming
    Replies: 23
    Last Post: 28th May 2007, 21:44

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.