Telnet 服务器:保持连接打开是好习惯吗?

Telnet server: is it good practice to keep connections open?

我在一个 NetHack 克隆中工作,它应该像许多 NetHack 服务器一样通过 Telnet 进行游戏。正如我所说,这是一个克隆,所以它是在 Python.

从头开始​​编写的

我设置了我的套接字服务器,重用了我刚才写的 SMTP 服务器的代码,突然间我的注意力都跳到了这一行代码:

s.listen(15)

我的服务器设计为能够同时连接到 15 个客户端,以防与任何客户端的数据交换时间过长,但理想情况下 listen(1)listen(2) 就足够了。但是这个案例不一样。

正如 Alt.org when you telnet their NetHack servers, people connected to my server should be able to play my roguelike remotely, through a single telnet session, so I guess this connection should not be interrupted. Yet, I've read here 那样

[...] if you are really holding more than 128 queued connect requests you are a) taking too long to process them or b) need a heavy-weight distributed server or c) suffering a DDoS attack.

在这里进行的更好的做法是什么?我应该保持每个连接打开直到连接的用户断开连接还是有任何其他方式?我应该选择 listen(128)(或者我的系统的 socket.SOMAXCONN 是什么)还是一种不好的做法?

listen(number) 中的

number 请求限制了 未决 连接请求的数量。

从 OS 收到初始 SYN 请求到您调用 accept 套接字方法,连接请求处于 待处理。所以 number 不限制打开(已建立)的连接数,但它限制处于 SYN_RECV 状态的连接数。

不接听传入连接是个坏主意,因为:

  • 客户端将重新传输 SYN 请求,直到收到应答 SYN
  • 当您的服务器不可用而它刚好在队列中时,客户端无法区分情况。

更好的办法是在连接时回答,但向客户端发送一些带有拒绝原因的消息,然后关闭连接。