0

I'm using Qt 5.6. I'm working on a simple chat application and came across several places online all saying you have to create a new thread to contain QTcpSocket to handle all the new connections in a new thread.

I'm trying to figure out why you can't just use a QList of QTcpSockets to handle all the connections. Can someone please explain to me why?

If I'm mistaken, and that is a perfectly fine thing to do, please tell me that as well.

Thanks in advance.

Flare Cat
  • 591
  • 2
  • 12
  • 24
  • You **can** have a `QList` of `QTcpSocket`s to handle all the connections on the main thread. In fact, this solution is preferred in Qt. You don't have to create a new thread for each connection at all. – Mike Aug 28 '16 at 15:17
  • provide some links of the online places you are referring to. I don't get what do you mean by "*create a new thread to contain **`QTcpSocket`** to handle **all** connections*", How would you have a `QTcpSocket` that can handle all connections? did you mean a `QTcpServer`? – Mike Aug 28 '16 at 15:20
  • @Mike Oh, I meant to say I make a new thread for every `QTcpSocket`. Most notably, I heard it from voidrealm's qt playlist on YouTube. I made that really late at night so I was kind of tired. Thanks for your response. – Flare Cat Aug 28 '16 at 15:45

1 Answers1

1

Using of multithreading tcp server or using one thread tcp server depends on your task. In some tasks enough one thread and you can "just use a QList of QTcpSockets". Main characteristics of such tasks are:

  1. Small number of simultaneous incoming connections. (Critical number of simultaneous incoming connections when you must use multithreading also depends of your task. But I am convinced, that when you have more than 10 simultaneous incoming connections, you should take thought about multithreading.)
  2. Low network load / low network interactions.

In other case it's better to do your tcp server with opportunity of making some jobs in parallel. How many connections you will be have in one thread depends of your task, but make very high number of threads is also bad idea, because then you get problems with performance. Many time will be spend to context switching.

I think, in your case you can use the simplest variant and "just use a QList of QTcpSockets"

Kirill Chernikov
  • 1,387
  • 8
  • 21