Results 1 to 7 of 7

Thread: Thread, Timer and Socket. Comuication problem

  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 Thread, Timer and Socket. Comuication problem

    Hi,

    I have this problem:

    -One thread is continously doing some work and when it finish one loop, it emits a signal to own class in the main thread.
    -The main thread has the own class that takes the signal and the properly slot is called. It uses a QQueue and enqueue this message.
    -This own class is a QThread and has a QTcpSocket. In "run" method, it asks the QQueue if there is a message to send, and if there is one, it sends using the socket.
    -This own class has the "readyRead" signal connected to a slot (in the thread).

    This works ok if only doing this, but I'm getting "QSocketNotifier: socket notifiers cannot be disabled from another thread". I have readed that this is not a good solution.

    I modified the own class to not inherit from QThread and the QSocketNotifier error has gone. Now the own class enqueue the message and sends the message(the message is enqueued because the response message have to be compared with the sended message).

    But I have more things:

    -In the main thread there is a QTimer that every 5 seconds tells the onw class to send a message, so it enqueue it and send it.
    -Then, when the "readyRead" signal is emited that calls the slot "readFromSocket", I read the corresponding bytes, dequeue the first sended message and compare them.

    It is like one thread create a quesion and the timer another one(different). By this I use a QQueue. Note that my class is not a Thread now.

    But this is not working as expected.

    Anyone knows how to do this?

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Thread, Timer and Socket. Comuication problem

    Very hard to tell with out seeing the code.
    Specially, since you might THINK you are doing something, but really doing something else, mostly due to problems with thread affinity, which sounds to me to be the problem her as well.

    It will be good if you could post relevant code.
    Specially where you create the objects you are talking about.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  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: Thread, Timer and Socket. Comuication problem

    Hi,

    Sorry, but I have 24 files only to manage the communication and 4 more files to manage the application and the thread. So, I can't attach the files here.
    I'm developing the Modbus protocol.

    The timer is in the main thread
    The class that has the socket is in the main thread
    The other thread emits a signal that is catched in the main thread usign Queued connection.

    Thanks,
    Òscar Llarch i Galán

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Thread, Timer and Socket. Comuication problem

    So, I can't attach the files here.
    That is why I said relevant code - which means showing in code snippets the same you were explaining in words.

    So lets try to make things clear:
    You have a socket and a polling timer in the main thread.
    You have a worker thread, that emits a signal, which fires a slot in the main thread that sends something through a socket is that correct?

    And then, again, what is the problem you are having?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    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: Thread, Timer and Socket. Comuication problem

    Hi,

    You have a socket and a polling timer in the main thread.
    You have a worker thread, that emits a signal, which fires a slot in the main thread that sends something through a socket is that correct?
    Yes, this is correct.

    And then, again, what is the problem you are having?
    The class that contain the socket has a QQueue that enqueue the message sended, so the messages enqueued by the timer and the thread(that will be different) stay in a FIFO.
    Then, when the socket recives a response from the client, I have to verify that the message sended corresponds to the response got. So dequeue the sended message and compare to the recived.

    May this work? I think yes, but I'm having troubles:
    Imagin that the response of the client tells me to stop the worker thread, so I sotp it, but the timer is still sending messages. Then, one of the responses can tell me to start the thread anagin.

    Stops ok but it don't start again.

    Thanks,
    Òscar Llarch i Galán

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Thread, Timer and Socket. Comuication problem

    Imagin that the response of the client tells me to stop the worker thread, so I sotp it, but the timer is still sending messages. Then, one of the responses can tell me to start the thread anagin.
    Well, why can't you stop the time when you stop the worker thread?

    Stops ok but it don't start again.
    But where is the problem?
    If you receive a message from the socket to start the thread again, what stops you from restarting your thread again?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    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: Thread, Timer and Socket. Comuication problem

    Hi,

    I founded what is happening.

    The client is a third party software for testing modbus.
    In theory the TCP SLAVE let me send a lot of requests and the SLAVE will send the responses. But the third party software don't do this. It is not able to get a request if it has not sended the response of the previous request.

    I suposed it because of sleeping the worker thread makes it work.

    Now, I will try to force to wait until the response has arrived until a new request is sended.

    Thanks,
    Òscar Llarch i Galán

Similar Threads

  1. Thread(s) and socket, timer slots
    By stephdev1965 in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2006, 14:04

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.