Results 1 to 7 of 7

Thread: thread with socket?

  1. #1
    Join Date
    Dec 2007
    Posts
    129
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default thread with socket?

    i have part of my application that connects through other pc and send data using socket, as i add this to my main project the timeout of socket will block the gui, so my solution is put this part of program to a qthread..

    however after i do this
    Qt Code:
    1. thread = new Mythread();
    2. thread->start();
    To copy to clipboard, switch view to plain text mode 
    It will not wait for the socket to connect, the thread immediately exited so i add thread->wait()
    Qt Code:
    1. thread = new Mythread();
    2. thread->start();
    3. thread->wait();//this solve the problem however..
    To copy to clipboard, switch view to plain text mode 
    by doing this it will block the GUI, and it is not the thread i want after all..

    how to solve this?

  2. #2
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: thread with socket?

    Sockets can be used in blocking (synchronous) and non-blocking (asynchronous) fashion. Non-blocking use of sockets does not require threads because it relies on signals and event loops.

    There are two samples available:

    Blocking sockets

    http://doc.trolltech.com/4.4/network...uneclient.html

    Look at fortunethread.cpp

    Non-blocking sockets

    http://doc.trolltech.com/4.4/network-fortuneclient.html

    Look at client.cpp

    This document gives more background on these two usages of sockets: http://doc.trolltech.com/4.4/qtnetwo...and-qtcpserver

    And this document provides some information on using event loops within threads: http://doc.trolltech.com/4.4/threads...ead-event-loop

    It is hard to be more specific without knowing what MyThread::run() looks like.
    Last edited by yuriry; 24th September 2008 at 06:20.

  3. #3
    Join Date
    Dec 2007
    Posts
    129
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: thread with socket?

    yeah thanks..

    the code doesnt use qt socket but linux socket

    im trying to intergrate it to my qt app

    im not in my programming pc now..
    Qt Code:
    1. Mytread::run()
    2. {
    3. //connect using socket
    4. //what would be the diffrence if I add
    5. exec(); //here?
    6. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: thread with socket?

    I think exec() will not help you. You would only need to call it if you wanted to use sockets in a non-blocking way on a separate thread. I assume that you are using Linux sockets in a blocking manner (no select() calls). I would then keep a pointer to an instance of Mythread in an object that is created and used in GUI thread. Check BlockingClient.h for an example. When you receive data using Linux sockets, you need to pass the data to that GUI object. You can emit a signal that your GUI object can receive. See emit newFortune(...) in fortunethread.cpp. Also pay attention to different types of connections between objects: http://doc.trolltech.com/4.4/threads...across-threads Do not use direct connections without using some synchronization mechanism between Mythread and the GUI thread.
    Last edited by yuriry; 24th September 2008 at 17:36.

  5. #5
    Join Date
    Dec 2007
    Posts
    129
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: thread with socket?

    Quote Originally Posted by yuriry View Post
    You would only need to call it if you wanted to use sockets in a non-blocking way on a separate thread.
    yeah thats what i need.. so i will add exec() then?

    i will just call it pass some parameter and execute, I dont need the return data i dont even need to test if the socket connects or not I want to let the thread do that and the application do what it should do.. Calling the socket blocks the GUI so i put it in a thread but if i need to put thread->wait() it will block GUI too..

    im just avoiding to call it as binary but if it will block the GUI then i will reconsider it..

  6. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: thread with socket?

    Use QSocketNotifier.
    No need to create a separate thread. (You still may to so, if you want, though.)

    HTH

  7. #7
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: thread with socket?

    yeah thats what i need.. so i will add exec() then?
    exec() is only required if you want to start an event loop on a thread. If you used QTcpSocket in conjunction with readyRead signal then you would need an event loop. But if you use Linux sockets, and I assume you do NOT use select/poll, just read/write, then you use them in a blocking fashion and your code should be similar to this example http://doc.trolltech.com/4.4/network...uneclient.html Even if sockets are used there in a blocking way, GUI thread does not block because blocking read/write calls occur on a separate thread.

    And as caduel pointed out, there is a way to achieve non-blocking behavior without creating extra threads, but it seems that it is not an option for you because you are just integrating existing code and do not want to do many changes to it.

Similar Threads

  1. Socket on a Thread
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 7th May 2008, 15:56
  2. Thread, Timer and Socket. Comuication problem
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2008, 16:48
  3. socket in thread error!
    By alphaboy in forum Qt Programming
    Replies: 6
    Last Post: 18th November 2007, 12:15
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. How to write on a socket in another thread?
    By Valheru in forum Qt Programming
    Replies: 7
    Last Post: 12th October 2006, 10:52

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.