Answer the question
In order to leave comments, you need to log in
Can a TCP connection work with multiple clients at once?
Hello, I saw a topic on the 1st forum how a person wrote a server where he used a TCP connection and he cannot organize the connection of several clients at once, and that's what to be safe, I decided to ask the question: Is it possible to use the TCP protocol to keep several clients on 1 server, leading as you know, the TCP protocol is able to keep a connection with the mb client, its problem lies in the fact that this protocol does not support so many simultaneous connections?
Answer the question
In order to leave comments, you need to log in
TCP connection is a socket . An open socket binds a specific port on the local machine to a specific port on a specific remote machine (and nothing else). An open socket always connects two sides. There are no multiway sockets. But on one machine, as many sockets as you want can be opened.
The TCP server opens 1 server (LISTENING) socket for the entire time it is waiting for connections (binding it to a specific port number).
At the moment of connection of each next client, the ACCEPT function creates a new server socket intended for exchange with this particular client.
Total: at each moment of time, the server program keeps 1 LISTENING socket + N INCOMING sockets active for exchanging data according to the number of currently active client connections.
Vedas as you know, the TCP protocol is able to keep a connection with the client mb its problem lies in the fact that this protocol does not support so many simultaneous connections?is that a pun?!)
Of course it can, because all existing application-level protocols, such as HTTP, are based on TCP, in HTTP both the request and response are TCP packets, there are obviously many clients supported, just create your own stream with a read cycle for each client, in one more stream accept clients (accept) and create threads with cycles, and if you need to send a packet to the client outside the reading cycle (due to some action from the server), then one more thread is needed, or just in the main thread (GUI), and in In some libraries, such as Qt, threads are implemented "out of the box" in the classes themselves and it is enough to subscribe to signals (events).
So. I lay it out on the shelves:
A TCP connection is an already established connection between sockets. It cannot be multilateral.
On servers, the socket, after the connection, is quickly copied, pushed out into a separate thread, where it begins to serve the client that has connected to it, and the port, in the main thread, is again put into the connection waiting state and waiting for the next connection to be accepted.
Examples in different languages are available on Google for "multithreading server"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question