Results 1 to 5 of 5

Thread: Socket thread problem

  1. #1
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Socket thread problem

    Yes, against the expert's advice, I am using a separate thread for QTcpSocket to send data to a server.
    Here is why - it takes between 10 and 15 seconds to log into the server, receive a response, then send the data.
    If a separate thread is not used, the gui is frozen during this time which is unacceptable.

    The problem however is that when the data is sent to the server, and the send loop exited, and the signal sent back to the originating dialog to close, the slot in the dialog to close the operation never executes. So I am assuming the signal is never received. Using debug, I can see that the process is hung up in a looping nature having to do with mutex when the send loop in the thread is exited and the signal is sent. This is puzzling since there are no shared objects between this thread and the main thread.

    I did read "you are doing it wrong". Does this imply that simply moving the networking code to a new class and calling it from the thread class will solve the problem? It doesn't seem like it. Yes, I did try setting the connection as queued. No difference.

    Suggestions are welcome and thanks!

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

    Default Re: Socket thread problem

    Quote Originally Posted by waynew View Post
    Yes, against the expert's advice, I am using a separate thread for QTcpSocket to send data to a server.
    Here is why - it takes between 10 and 15 seconds to log into the server, receive a response, then send the data.
    If a separate thread is not used, the gui is frozen during this time which is unacceptable.
    Then use asynchronous API, nothing will block then.
    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.


  3. #3
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Socket thread problem

    I'm not sure how to do this Wysota. What would the structure look like? How can you be listening for the server response and still running the gui?

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

    Default Re: Socket thread problem

    You would connect to your socket's readyRead() signal or other signals appropriate to the situation, as the examples bundled with Qt show.
    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. The following user says thank you to wysota for this useful post:

    waynew (17th October 2011)

  6. #5
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Socket thread problem

    Thanks Wysota - I think I see what you mean. I am going to restructure this way and see how it works.
    I'm sure you will have some sage advice if I have problems

Similar Threads

  1. problem with Tcp socket reception in a thread...
    By medampudi in forum Qt Programming
    Replies: 5
    Last Post: 15th February 2013, 04:17
  2. Socket + thread problem
    By Daxos in forum Qt Programming
    Replies: 11
    Last Post: 13th July 2010, 11:28
  3. thread with socket?
    By triperzonak in forum Qt Programming
    Replies: 6
    Last Post: 25th September 2008, 16:21
  4. Socket on a Thread
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 7th May 2008, 15:56
  5. Thread, Timer and Socket. Comuication problem
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2008, 16:48

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
  •  
Qt is a trademark of The Qt Company.