简体   繁体   中英

How many clients can connect to one TCP Port

I've an application in c# windows forms through which I stream photo taken by a web cam in few seconds interval. The photo data get sent to a server listening on TCP port.

My question is if this application is installed on hundreds of computers, shall there be an issue to listen on one single port or should I assign a different port to each client? Keeping in mind that photos get sent after every few seconds.

Here is my code of server listener.

                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Bind(new IPEndPoint(IPAddress.Parse("some ip"),5001));

                Task.Factory.StartNew(() =>
                {
                    socket.Listen(500);
                    //socket listening code.

                }, TaskCreationOptions.LongRunning);

Thanks.

TCP Connections are characterised by four parameters - source IP Address and Port and destination IP Address and Port. Provided those four are unique, no confusion ensues.

Given that source's are either machines actually assigned unique addresses (more likely with IPv6) or are tunnelling through NAT devices (and thus asigned unique source ports), there's no need to do anything on the server side.


If you're thinking of assigning anything to each connected client (beyond the socket and some buffers), you're probably doing something wrong. Don't dedicate any exclusive resources to the clients.

See: the C10k problem .

Note that when you use a listener port, behind the scenes you end up using an ephemeral port for the established connection after the listener has handed it over - but from the connecting client's perspective , that doesn't matter - the listener port remains a single point of entry for all the eventual sockets.

FWIW: the web-sockets server that drives the real-time updates here on Stack Overflow currently averages 14,500 sockets per port, 3 ports per service instance, 9 service instances, for a total of about 392,000 sockets. We've seen it much higher at some points in the past. So yes, you can easily support a few hundred clients oa single listener port, but : it gets non-trivial the more sockets you want to support, and you absolutely shouldn't use theads per client if you're going over a very small number of clients.

Don't worry you can listen multiple sockets on a same port, so multiple clients can communicate on the same port.

For example, you probably know that when you access on a website, you're connecting to a server on the port 80 (or 443 for https), well... Fortunatly, a website can be accessed by multiple clients, and all on the same port ! ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM