Results 1 to 4 of 4

Thread: Challenging QTcpSocket reliability problem

  1. #1
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Question Challenging QTcpSocket reliability problem

    Hi everyone,

    I have found a strange behavior by QTcpSocket working on it.
    The explanation isn't easy, but I'll try it anyway.

    The idea was to create a thread in the application that works with a QTcpSocket to, of course, connect to a server.

    So, subclass QThread, and there what is needed for the socket.

    Now, depending on how and where QTcpSocket is declared, connection is successful or not.
    Details:
    considering the same code to connect, that is a simple
    Qt Code:
    1. socket->connectToHost("someserver",someport);
    To copy to clipboard, switch view to plain text mode 
    let's change the QTcpSocket declaration.

    1) as a local variabile in run() (QThread::run() reimplementation)
    Qt Code:
    1. QTcpSocket socket;
    2. socket.connectToHost("server...",port);
    To copy to clipboard, switch view to plain text mode 
    and in this way works fine.

    2) as a dinamically a allocated pointer in thread class (member pointer)
    Qt Code:
    1. class mysocketthread:public QThread
    2. {
    3. private:
    4. QTcpSocket *socket;
    5. ...
    6. };
    7.  
    8. mysocketthread::mysocketthread()
    9. {
    10. socket=new QTcpSocket();
    11. ...
    12. }
    13.  
    14. void mysocketthread::run()
    15. {
    16. socket->connectToHost("server...",port);
    17. ...
    18. }
    To copy to clipboard, switch view to plain text mode 
    and in this way keeps failing the connection, giving a constant "Connection refused".

    Maybe I'm missing something, but I think of this as quite strange... isn't it?
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Challenging QTcpSocket reliability problem

    Quote Originally Posted by Raccoon29 View Post
    Maybe I'm missing something, but I think of this as quite strange... isn't it?
    It's not that strange but a clear misuse of QTcpSocket. QTcpSocket is not thread safe.

    When you instantiate "mysocketthread" in the main thread, the constructor of "mysocketthread" gets executed immediately in the main thread. Later on when you call QThread::start(), the run() method will get executed in the newly started thread context. This means that "mysocketthread::mysocketthread()" and "mysocketthread::run()" are executed in different threads. Be careful with such code.

    Anyway, what do you need a thread for in the first place? QTcpSocket works asynchronously.
    J-P Nurmi

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

    Raccoon29 (13th January 2009)

  4. #3
    Join Date
    Dec 2008
    Posts
    29
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Challenging QTcpSocket reliability problem

    If you need it (for example for a driven long data transfers or just because more threads in one application need to communicate via one connection with other application - it is similar by some database interfaces, for hiding concrete behaviour, etc.), you can use the common pattern of private object created in run() method of a QThread descendant. The QThread descendant serves as a wrapper, that envelops direct calls from other threads and redirects them asynchronnously to private object with using signal-slot mechanism. The private object manages all the stuff around the tcp or database connection.. Locking access to this private object is another story.. and depends on livecycle and use of your wrapper/private object.

  5. #4
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Challenging QTcpSocket reliability problem

    Quote Originally Posted by jpn View Post
    It's not that strange but a clear misuse of QTcpSocket. QTcpSocket is not thread safe.

    When you instantiate "mysocketthread" in the main thread, the constructor of "mysocketthread" gets executed immediately in the main thread. Later on when you call QThread::start(), the run() method will get executed in the newly started thread context. This means that "mysocketthread::mysocketthread()" and "mysocketthread::run()" are executed in different threads. Be careful with such code.
    Oh I see, so now I really understand the meaning of that "thread safe" attribute
    Eventually I was missing something :P
    Thanks,thanks,thanks!
    By the way, I used thread for a sort of experiment...

    @seim thank you for the suggestion, but I think I'll wipe out threads nearing some sockets
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

Similar Threads

  1. Problem in QTcpSocket
    By navi1084 in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2008, 12:12
  2. Problem with QTcpSocket in QThread
    By Raistlin in forum Qt Programming
    Replies: 8
    Last Post: 6th October 2007, 12:23
  3. QTcpSocket speed problem
    By benelgiac in forum Qt Programming
    Replies: 4
    Last Post: 1st May 2007, 13:50
  4. problem with QTcpSocket
    By SuperSonik in forum Qt Programming
    Replies: 8
    Last Post: 31st January 2007, 16:00
  5. QTcpSocket disconnection problem
    By erdi in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2006, 21:50

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.