简体   繁体   English

服务器可以处理的不同端口上有多少个tcp连接?

[英]How many tcp connections on different ports a sever can handle?

I am designing a server client app in C#. 我正在用C#设计一个服务器客户端应用程序。 the client connect and communicate with the sever threw tcp socket. 客户端连接并与服务器通信扔掉了tcp套接字。

in the server side I am using the socket.accept() method in order to handle new connection from client. 在服务器端我使用socket.accept()方法来处理来自客户端的新连接。 when client is connecting, the server use a random port in order to communicate with the client. 当客户端连接时,服务器使用随机端口以与客户端通信。

so my question is.. how many clients the server can receive in this kind of form? 所以我的问题是..服务器可以以这种形式接收多少客户端? is there another form that I should use in order to handle lots of clients? 我应该使用另一种形式来处理大量客户吗?

This is practically limited by the OS. 这实际上受操作系统的限制。 You have to test this. 你必须测试这个。 On Windows you must use fully asynchronous socket IO at this scale. 在Windows上,您必须使用此规模的完全异步套接字IO。 You will probably be limited by memory usage. 您可能会受到内存使用的限制。

On a TCP level there is no practical limit. 在TCP级别上没有实际限制。 There can be one connection for each combination of (server port, server ip, client port, client ip) . 每个组合(server port, server ip, client port, client ip)可以有一个连接。 So with one server port and one server ip you can serve an unlimited amount of clients as long as they have less than 65k connections per client. 因此,只需一个服务器端口和一个服务器ip,就可以为每个客户端提供少于65k的连接,从而为无限量的客户端提供服务。

You do not need to pick a random port on the server. 您无需在服务器上选择随机端口。 This is a common misconception. 这是一个普遍的误解。

in the server side i am using the socket.accept() method in order to handle new connection from client. 在服务器端我使用socket.accept()方法来处理来自客户端的新连接。 when client is connecting, the server use a random port in order to communicate with the client. 当客户端连接时,服务器使用随机端口以与客户端通信。

Not unless you open another, pointless, connection from server to client, and you won't be doing that for firewall reasons. 除非你打开从服务器到客户端的另一个毫无意义的连接,否则你不会出于防火墙的原因而这样做。 The accepted socket uses the same local port number as the listening socket. 接受的套接字使用与侦听套接字相同的本地端口号。 Contrary to several answers and comments here. 与此处的几个答案和评论相反。

Your question is therefore founded on a misconception. 因此,你的问题建立在误解之上。 Whatever you run out of, and it could be memory, thread handles, socket handles, socket buffer space, CPUs, CPU power, virtual memory, disk space, ..., it won't be TCP ports. 无论你用完什么,它可能是内存,线程句柄,套接字句柄,套接字缓冲区空间,CPU,CPU电源,虚拟内存,磁盘空间......,它不会是TCP端口。

EDIT Adherents of the new-random-port theory need to explain the following netstat output: EDIT新随机端口理论的netstat需要解释以下netstat输出:

TCP    127.0.0.4:8009         0.0.0.0:0              LISTENING
TCP    127.0.0.4:8009         127.0.0.1:53777        ESTABLISHED
TCP    127.0.0.4:8009         127.0.0.1:53793        ESTABLISHED
TCP    127.0.0.4:8009         127.0.0.1:53794        ESTABLISHED
TCP    127.0.0.4:8009         127.0.0.1:53795        ESTABLISHED
TCP    127.0.0.4:8009         127.0.0.1:53796        ESTABLISHED
TCP    127.0.0.4:8009         127.0.0.1:53798        ESTABLISHED
TCP    127.0.0.4:8009         127.0.0.1:53935        ESTABLISHED

and show where in RFC 793 it says anything about allocating a new port to an accepted socket, and where in the TCP connect-handshake exchange the new port number is conveyed. 并显示在RFC 793中它所说的关于为接受的套接字分配新端口的任何内容,以及在TCP连接握手交换中传送新端口号的位置。

You may like to see this question I asked in similar vein: https://softwareengineering.stackexchange.com/questions/234672/is-there-are-problem-holding-large-numbers-of-open-socket-connections-for-length , and particularly some of the comments. 您可能希望以类似的方式看到我提出的这个问题: https//softwareengineering.stackexchange.com/questions/234672/is-there-are-problem-holding-large-numbers-of-open-socket-connections-for - 长度 ,特别是一些评论。

The answer seems to be that there is no practical limit. 答案似乎是没有实际限制。 The combination of receive port and send port must be unique, and each of them can have 64K values. 接收端口和发送端口的组合必须是唯一的,并且每个端口都可以具有64K值。 The total number of combinations is extremely large. 组合总数非常大。 There really are servers out there with extremely large numbers of open connections, but to get there you have to solve a number of other interesting problems. 确实存在具有极大数量的开放连接的服务器,但要实现这一目标,您必须解决许多其他有趣的问题。 The question above contains a link to an article about a million connection server. 上面的问题包含一篇关于一百万个连接服务器的文章的链接。 See also How to retain one million simultaneous TCP connections? 另请参阅如何保留一百万个并发TCP连接? . And do a web search for the C10K problem. 并在网上搜索C10K问题。

What you probably cannot do is use synchronous ports and threads because you run into thread limits, not port limits. 您可能不能做的是使用同步端口和线程,因为您遇到了线程限制,而不是端口限制。 You have to use asynchronous ports and a thread pool. 您必须使用异步端口和线程池。 And you will have to write one to throw away, just to find out how to do it. 你必须写一个扔掉,只是为了找到如何做到这一点。

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

相关问题 连接太多时,传出客户端TCP端口将被阻塞 - Outgoing client TCP ports get blocked when there are too many connections 如何处理TCP C#套接字服务器中的多个活动连接? - How to handle multiple active connections in a TCP C# socket server? 每个端口接受多个连接时如何监听多个TCP端口 - How to listen to multiple TCP ports when each one accepts multiple connections 无法停止异步TCP服务器Windows服务 - Can not stop async TCP sever Windows service 如何创建TCP客户端并管理许多TCP连接(可能使用SocketAsyncEventArgs和Reactive Extensions)? - How to create TCP client and manage many TCP connections (probably with SocketAsyncEventArgs and Reactive Extensions)? 如何在单个事务范围内处理与不同数据库的连接 - How can i handle connections to different databases inside a single transaction scope 连接到两个不同端口上的服务器的TCP套接字 - TCP Sockets connecting to a server on two different ports 有效且高效地管理许多TCP连接 - Manage many TCP connections effectively and efficiently WCF的ClientBase如何 <TChannel> 处理时处理TCP连接? - How does WCF's ClientBase<TChannel> handle TCP connections when Disposed? Azure和本地TCP连接数非常不同 - Azure and local number of TCP connections are very different
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM