I am writing a server for chat. Your protocol. Familiar (friend) client writes. There was a question how to organize servicing of several clients.

First option. The eavesdropping of each socket is a separate thread. Hence the question: "How much will it be resource-intensive compared with the second option?"

The second option. 3 threads. The first accepts new connections and adds users for example to the "vector". The second runs through the "vector", taking the data. The third sends in the same way.

I will also accept the advice on the implementation algorithm.

  • In the new sockets throw every connection. - Sergey
  • I would learn the source code of Jabber. - Sergey

1 answer 1

How resource intensive depends on the number of clients. If there are not many of them, choose any option, it will work, if correctly implemented. Otherwise, there are disadvantages to both options:

  1. For the first - a lot of threads. Time to create a stream is also spent, etc.
  2. For the second - the size of the buffer. Here are the disadvantages: how to store data to send (received from other customers); processing speed of each of the vectors (lists), etc.
  3. And if you try to combine both options? Allocate one thread, for example, for 10 clients, will have to suffer with processing, but you compensate for the shortcomings of their options.
  4. And would not go to a new level? Organization of p2p chat with the server. The server only knows who is online and their identifiers, and the clients exchange messages without server participation.