Results 1 to 3 of 3

Thread: QThread and QTcpSocket

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QThread and QTcpSocket

    Hi,

    If I use a socket into a QThread, and the "run()" method can lock itself depending on a QWaitCondition, the socket will continue reciving the network data until the thread is locked?

    I want a QThread that sends messages(depending on the app state, user interaction, ...), but the QThread must not send any message until recives the confirmation of the previous message:

    QThread(with QTcpSocket) Client
    |-->send message------------------------------------------------>recive message
    |
    | locked untiil recive message (doing some jobs)
    |
    |--- unlock thread<-------------------------------------------------send confirmation


    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jan 2008
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread and QTcpSocket

    Well, I'm not sure if i really know what you mean. But it looks like you want the thread to send a message through the socket and have it block until it receives a reply from the network client (or gui?).

    First of all, why do you have to use a thread for this? It is not doing anything useful except sleeping and sending a message once it gets a reply. You could do it easily using the normal signal/slots with a non-threaded class. In case you didnt provide the full info and it does do something instead of waiting there are 2 solutions:

    1. just give your thread an event-loop with the exec() function in your run method and connect the readyRead() signal of the socket to a slot in your thread. This is the non-blocking way.
    2. in your run() method do something like:
    Qt Code:
    1. forever {
    2. socket->write(message);
    3. socket->waitForBytesWritten();
    4. socket->waitForReadyRead();
    5. read_reply();
    6. }
    To copy to clipboard, switch view to plain text mode 


    Can you maybe further explain why you need this thread or what it really does? Because i really think you dont need a thread for your task. Threads are a pain anyway, so evade them whenever possible! Just my 2 cents ;-)

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread and QTcpSocket

    Hi,

    Thanks for reply but I had solved yet.

    First of all, why do you have to use a thread for this?
    Because there are other Threads that are creting messages and them send the message to the communication thread that have to send it.

    But don't worry about it, as I said, I solved it.

    Thanks,
    Òscar Llarch i Galán

Similar Threads

  1. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2008, 13:06
  2. Problem with QTcpSocket in QThread
    By Raistlin in forum Qt Programming
    Replies: 8
    Last Post: 6th October 2007, 12:23
  3. Strange QTcpSocket behavior (debugged with Ethereal)
    By mdecandia in forum Qt Programming
    Replies: 23
    Last Post: 28th May 2007, 20:44
  4. Problems with QThread and QTcpSocket
    By cookiem in forum Qt Programming
    Replies: 6
    Last Post: 2nd November 2006, 08:25
  5. QThread and QTcpSocket problem
    By probine in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2006, 09:57

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.