简体   繁体   English

多少个客户端可以连接到一个TCP端口

[英]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. 我在c#Windows窗体中有一个应用程序,通过它我可以在几秒钟的间隔内流式传输网络摄像头拍摄的照片。 The photo data get sent to a server listening on TCP port. 照片数据被发送到侦听TCP端口的服务器。

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. TCP连接具有四个参数-源IP地址和端口以及目标IP地址和端口。 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. 假设源是实际分配了唯一地址的机器(更可能是使用IPv6),或者是通过NAT设备建立隧道(因此分配了唯一源端口),则无需在服务器端执行任何操作。


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 . 请参阅: C10k问题

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. FWIW:在这里,在Stack Overflow上驱动实时更新的Web套接字服务器当前平均每个端口14,500个套接字,每个服务实例3个端口,9个服务实例,总共约392,000个套接字。 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. 所以是的,您可以轻松地通过一个侦听器端口支持数百个客户端, 但是 :您想要支持的套接字越多,它就变得无关紧要了,如果您要访问的客户端很小,则绝对不要为每个客户端使用theads。客户数量。

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 ! 例如,您可能知道,当您访问网站时,您正在连接到端口80上的服务器(对于https,则为443)...幸运的是,一个网站可以被多个客户端访问,并且所有同一个端口! ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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