简体   繁体   English

在多个端口上监听

[英]Listening on multiple ports

I'm playing a bit with Twisted and created a simple 'server'. 我在玩Twisted,并创建了一个简单的“服务器”。
I'd like to let the server listening on multiple ports (1025-65535) instead of a single port. 我想让服务器侦听多个端口(1025-65535),而不是单个端口。
How can i do this ? 我怎样才能做到这一点 ?

My code: 我的代码:

from twisted.internet.protocol import Protocol,ServerFactory
from twisted.internet import reactor

class QuickDisconnectProtocol(Protocol): 
    def connectionMade(self): 
        print "Connection from : ", self.transport.getPeer()
        self.transport.loseConnection() # terminate connection


f = ServerFactory()
f.protocol = QuickDisconnectProtocol
reactor.listenTCP(6666,f)
reactor.run()

Already tried this: 已经尝试过:

for i in range (0, 64510):
    reactor.listenTCP(1025+i,f)

reactor.run()

But received an error: 但是收到一个错误:

Traceback (most recent call last):
  File "Server.py", line 14, in <module>
  File "/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 436, in listenTCP
  File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 641, in startListening
twisted.internet.error.CannotListenError: Couldn't listen on any:2044: [Errno 24] Too many open files.

Each listening port requires a file descriptor ("open file"), and each file descriptor takes up one element of your maximum file descriptors quota. 每个侦听端口都需要一个文件描述符(“打开文件”),并且每个文件描述符占用最大文件描述符配额的一个元素。

This stack overflow question has an answer explaining how to raise this limit on Linux, and this blog post has resources as to how to do it on OS X. 这个堆栈溢出问题有一个答案,解释了如何在Linux上提高此限制, 这篇博客文章提供了有关如何在OS X上执行此操作的资源。

That said, the other respondents who have told you that this is not a particularly sane thing to do are right. 也就是说,其他告诉过您这不是一件特别理智的事情的受访者是正确的。 In particular, your operating system may stop working if you actually go all the way up to 65535, this overrules the entire ephemeral port range , which means you may not be able to make TCP client connections from this machine any more. 特别是,如果您实际一直上升到65535,则操作系统可能会停止工作,这会覆盖整个临时端口范围 ,这意味着您可能无法再从该计算机建立TCP客户端连接。 So it would be good to explain in your question why you are trying to do this. 因此,最好在您的问题中解释为什么要尝试这样做。

The usual solution is to have one listening port ( chosen by the server! ). 通常的解决方案是只有一个侦听端口(由服务器选择!)。 If you want each client on its own port, then the server chooses the port, starts listening on it, and replies to the client with the port it will use for further requests. 如果希望每个客户端使用其自己的端口,则服务器选择该端口,开始侦听该端口,并使用将用于进一步请求的端口回复该客户端。

It is not a real good use of port resources! 并不是很好地利用端口资源! If the server needs to keep state information for each client then it should issue a unique ID to each client when the client first connects and the client should use this ID for every request to the server. 如果服务器需要保留每个客户端的状态信息,则当客户端首次连接时,服务器应向每个客户端发出唯一的ID,并且客户端应将此ID用于对服务器的每个请求。

However, with a little care, you can often design the system so that the server does not need to keep separate state information for each client. 但是,您只要稍加注意,就可以经常设计系统,以使服务器不需要为每个客户端保留单独的状态信息。

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

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