Operations 10 min read

Setting Up a Million‑Connection TCP Test on Linux (Server and Client Configuration)

This guide walks through preparing a Linux server and client, adjusting kernel and user limits, configuring multiple IP addresses, and running PHP scripts to create and monitor up to one million concurrent TCP connections for performance testing.

Refining Core Development Skills
Refining Core Development Skills
Refining Core Development Skills
Setting Up a Million‑Connection TCP Test on Linux (Server and Client Configuration)

After reading previous articles on achieving a million TCP connections with a single server and client, the author provides the complete source code and step‑by‑step instructions for reproducing the experiment.

The first method described runs a single server process while using many client IPs; the second method (multiple server processes) will be covered later.

1. Server Preparation

1.1 Increase maximum file handles

Edit /etc/sysctl.conf to set system‑wide limits:

# vi /etc/sysctl.conf
fs.file-max=1100000
fs.nr_open=1100000

Apply with sysctl -p and verify using sysctl -a.

Then raise user‑level limits in /etc/security/limits.conf (applied to all users):

# vi /etc/security/limits.conf
*  soft  nofile  1010000
*  hard  nofile  1010000
Note: hard nofile must be smaller than fs.nr_open to avoid login failures.

Open a new terminal and confirm with ulimit -n (should show 1010000).

1.2 Start the server php server.php 0.0.0.0 8090 Verify listening with netstat -nlt | grep 8090.

2. Client Preparation

2.1 Adjust usable port range

Linux default ports are ~30,000; increase to cover 50,000‑65,000 connections:

# vi /etc/sysctl.conf
net.ipv4.ip_local_port_range = 5000 65000

Apply with sysctl -p.

2.2 Increase file limits for the client

Set system‑wide limits to 1,100,000 and per‑process fs.nr_open to 60,000:

# vi /etc/sysctl.conf
fs.file-max=1100000
fs.nr_open=60000

Apply and verify as before.

Configure user limits in /etc/security/limits.conf for each client process (5 × 10⁴ files per process, set to 55 000):

# vi /etc/security/limits.conf
*  soft  nofile  55000
*  hard  nofile  55000

Confirm with ulimit -n (should show 55000).

2.3 Choose new IP addresses for the client

Because a single client IP can open at most ~64 k connections, configure multiple IP aliases on one machine using ifconfig (e.g., eth0:1, eth0:2, … up to eth0:20). ifconfig eth0:1 CIP1 netmask 255.255.248.0 up Ensure the chosen IPs do not conflict with other devices on the LAN.

2.4 Apply the new IPs in the test code

Edit clientd.php to fill the $ips array with the selected addresses. Verify they are unreachable in the LAN with the provided ping tool: php clientd.php ping When all pings return false, bring the interfaces up: php clientd.php ifup Check with ifconfig that the aliases appear.

3. Run the Connection Test

Update clientd.php with the server IP and port, then start the connections: php clientd.php start In another terminal, monitor the number of ESTABLISHED sockets: watch "ss -ant | grep ESTABLISH" If the count exceeds 1,000,000, the experiment succeeded.

During the run, you may need to stop and restart the client ( php clientd.php stop) or the server (Ctrl‑C) and wait for the port to be released.

After success, examine memory usage:

$ cat /proc/meminfo
... (focus on Slab memory)

Use slabtop to see kernel object allocations.

4. Clean Up

Terminate the server with Ctrl‑C and stop the client: php clientd.php stop Remove the temporary IP aliases: php clientd.php ifdown The source code for the whole experiment is available at:

https://github.com/yanfeizhang/coder-kung-fu/tree/main/tests/network/test01

Additional related articles are linked at the end of the original post.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

TCPLinuxServer Configuration
Refining Core Development Skills
Written by

Refining Core Development Skills

Fei has over 10 years of development experience at Tencent and Sogou. Through this account, he shares his deep insights on performance.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.