Reputation: 2663
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.
Upvotes: 0
Views: 3200
Reputation: 1172
Don't worry you can handle multiple incoming connections on a same port, so that multiple clients can communicate with the server.
For example, you probably know that when you access a website, you're connecting to a server on the port 80 (or 443 for https), fortunately, a website can be accessed by multiple browsers, all reaching the same remote port ! ;)
Upvotes: 2
Reputation: 1062810
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 o a 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.
Upvotes: 2
Reputation: 239664
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.
Upvotes: 4