In theory, each connection is something about (src.ip, src.port, dst.ip, dst.port)
, so the number of connections per listening port could accept is unlimited only if there are enough clients.
However, I did an experiment of which the result is not such as I expect. After the number of connection reach 65520, no more connection could be established because not every SYN
segment of new connection is replied to with an ACK
segment. I've tried to establish connection from different computer with different IP address and the result is all the same.
Does anyone know what's the problem? What is the bottleneck resulting in the limit?
Below is some related configuration of my server:
- release: Ubuntu 18.04.2 LTS
- Python: 3.6.7
- CPU usage: less than 10%
- bandwidth usage: less than 10%
- memory usage: less than 10%
cat /proc/sys/fs/file-max
: 197344ulimit -n
: 100000
Here is the server code in python in my experiment:
import selectors
import socket
sel = selectors.DefaultSelector()
def accept(sock, mask):
conn, addr = sock.accept() # Should be ready
conn.setblocking(False)
sel.register(conn, selectors.EVENT_READ, read)
def read(conn, mask):
try:
data = conn.recv(1000) # Should be ready
if not data:
sel.unregister(conn)
conn.close()
except ConnectionResetError:
sel.unregister(conn)
conn.close()
sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', 8001))
sock.listen(100)
sock.setblocking(False)
sel.register(sock, selectors.EVENT_READ, accept)
while True:
events = sel.select()
for key, mask in events:
callback = key.data
callback(key.fileobj, mask)
print('current connections:', len(sel.get_map())-1)
Here is the client code to establish connections:
import socket
def build(host, port, number):
connections = []
for _ in range(number):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
connections.append(s)
return connections
c1 = build(host='homework', port=8001, number=10000)