0

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: 197344
  • ulimit -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)
user207421
  • 305,947
  • 44
  • 307
  • 483
dae
  • 589
  • 2
  • 6
  • 20
  • 1
    Also see [Maximum number of TCP connections in Python?](https://stackoverflow.com/q/7748898/608639), [How many socket connections possible?](https://stackoverflow.com/q/651665/608639) and [How many sockets can be created from a port?](https://stackoverflow.com/q/700594/608639) – jww Nov 18 '19 at 02:38
  • @jww These are not my condition. No exception has been raised in server side and the only error in client side is timeout. – dae Nov 18 '19 at 02:46
  • Also see [Increasing the maximum number of TCP/IP connections in Linux](https://stackoverflow.com/q/410616/608639) and the discussion of outbound ports. – jww Nov 18 '19 at 02:51
  • @jww I don't think the problem is about outbound ports because I've tried to establish connection from different computer with different ip. – dae Nov 18 '19 at 03:33
  • 1
    try to increase net.netfilter.nf_conntrack_buckets – Maxim Sagaydachny Nov 28 '19 at 15:39
  • @MaximSagaydachny It works after I increase net.netfilter.nf_conntrack_buckets. Thank you very much! – dae Dec 08 '19 at 05:27

0 Answers0