PDA

View Full Version : Threaded echo server - confused about signals/slots within a thread



ocgltd
28th September 2013, 03:54
I used the Threaded Fortune Server as a base, and created a threaded echo server. While it compiles, it has a problem that I think is very basic/foundational. (It crashes when I write to the socket with a message about creating a thread for a different parent, but I won't get into that here).

In my single thread I connect a readyRead signal from the tcpsocket to a readCommand slot inside the same thread. From what I've read, you should not have a signal from inside a thread connect to a slot in the same thread. Is that right?

If that's the problem, how should I design this server? Do I have to create 2 threads for each connection? (One for when the incoming connection is established, and a second to read/write from that connection?) And how would I get the tcpSocket info passed from one thread to the other. Confused!!

Thanks!

anda_skoa
28th September 2013, 10:10
I used the Threaded Fortune Server as a base, and created a threaded echo server. While it compiles, it has a problem that I think is very basic/foundational. (It crashes when I write to the socket with a message about creating a thread for a different parent, but I won't get into that here).

If you get a warning like that then this is something to investigate. This is very likely contributing to the problem.



In my single thread I connect a readyRead signal from the tcpsocket to a readCommand slot inside the same thread.

So the socket and the receiver object belong to the same thread?


From what I've read, you should not have a signal from inside a thread connect to a slot in the same thread. Is that right?
No. If that were true it would be impossible to write single threaded applications.



If that's the problem, how should I design this server?

Unless it is an exercise for multithreading my suggestion would be to do it single threaded.


Do I have to create 2 threads for each connection?
No, do not access a socket from two different threads. One per connection is more than enough, i.e. one often doesn't need a new thread per connection.

Cheers,
_