Search code examples
pythonsocketsserverconnectiontelnet

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


I'm working in a NetHack clone that is supposed to be playing through Telnet, like many NetHack servers. As I've said, this is a clone, so it's being written from scratch, on Python.

I've set up my socket server reusing code from a SMTP server I wrote a while ago and all of suddenly my attention jumped to this particular line of code:

s.listen(15)

My server was designed to be able to connect to 15 simultaneous clients just in case the data exchange with any took too long, but ideally listen(1) or listen(2) would be enough. But this case is different.

As it happens with 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 that

[...] 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.

What is the better practice to carry out here? Should I keep every connection open until the connected user disconnects or is there any other way? Should I go for listen(128) (or whatever my system's socket.SOMAXCONN is) or is that a bad practice?


Solution

  • number in listen(number) request limits number of pending connect requests.

    Connect request is pending from initial SYN request received by OS until you called accept socket method. So number does not limits open (established) connection number but it limits number of connections in SYN_RECV state.

    It is bad idea not to answer on incoming connection because:

    • Client will retransmit SYN requests until answer SYN is received
    • Client can not distinguish situation when your server is not available and it just in queue.

    Better idea is to answer on connection but send some message to client with rejection reason and then close connection.