TCP Servers: Listening Sockets, Binding, the Accept Queue, SYN Cookies and SYN Floods
How a TCP server receives connections: wildcard, specific and dual-stack listeners, how the 4-tuple separates clients on one port, the SYN and accept queues and what happens when they fill, and how SYN cookies keep a server reachable during a spoofed SYN flood, all measured in the lab.
kvi5hnuCode Sprout team member14 min readOn this page
Every TCP connection starts with a server that has called listen(). This article looks at the server side: which connections a listening socket accepts, how many clients can share one port, what the kernel does with connections before the application accepts them, and how a server survives a flood of fake connection requests.
Listening sockets and the 4-tuple
A server creates a socket, binds it to a local address and port, and calls listen(). The socket enters LISTEN and never carries data itself. For every completed handshake, the kernel creates a new socket for the connection, with the same local port.
In the lab, the server had two addresses, 198.51.100.10 and 198.51.100.11, and three listeners:
- port 8080 bound to the wildcard address 0.0.0.0, accepting connections to any local IPv4 address,
- port 8888 bound to one specific address, 198.51.100.10,
- port 2222 bound to the IPv6 wildcard
::, a dual-stack socket.
LISTEN 0 128 *:2222 *:*
LISTEN 0 128 0.0.0.0:8080 0.0.0.0:*
LISTEN 1 128 198.51.100.10:8888 0.0.0.0:*
The Recv-Q column of a listening socket shows connections waiting to be accepted, and Send-Q shows the backlog limit. The 8888 listener had 1 waiting, because the test program never accepted from it. The client opened connections to each. The server's ss then listed them, with the receive and send queues first, then the local and remote address and port:
0 0 198.51.100.10:8080 192.0.2.10:51704
0 0 198.51.100.10:8888 192.0.2.10:36680
0 0 198.51.100.11:8080 192.0.2.10:48422
0 0 [::ffff:198.51.100.10]:2222 [::ffff:192.0.2.10]:46028
Several things show up here:
- Many connections share one local port. Both 8080 connections have local port 8080. TCP tells connections apart by all four values: local address, local port, remote address and remote port. A segment is delivered to the connection whose 4-tuple matches it exactly. Only SYNs that match no connection go to the listening socket.
- The local address is filled in per connection. The wildcard listener received one connection on each server address, and each connection records which one.
- A dual-stack socket accepts IPv4. The connection to port 2222 shows IPv4 addresses in the IPv4-mapped IPv6 form
::ffff:198.51.100.10. That's the default becausenet.ipv6.bindv6onlyis 0. A program can set theIPV6_V6ONLYsocket option to accept IPv6 only.
A specifically bound listener accepts only connections to its own address. When the client connected to port 8888 on the other address, the kernel answered with a reset, exactly as for a closed port:
connect to 198.51.100.10:8888 OK
connect to 198.51.100.11:8888 failed: [Errno 111] Connection refused
net.ipv6.bindv6only = 0
When both a specific and a wildcard listener exist on the same port, the more specific one wins for its address. The sockets API offers no way for a listening TCP socket to accept only one remote address. A server must accept the connection and then check the peer's address, and by then the client's handshake has already succeeded. Firewalls are the right tool for restricting clients before the handshake.
The SYN queue and the accept queue
The kernel completes handshakes without the application's involvement. On Linux, a listening socket has two queues, and the diagram shows how a connection moves through them:

- The SYN queue holds half-open handshakes in SYN-RECV: the SYN has arrived and the SYN-ACK has gone out, and the ACK hasn't come back yet. Linux stores these as small request structures, not full sockets.
- The accept queue holds connections that have completed the handshake and are ESTABLISHED, waiting for the application to call
accept().
The backlog argument of listen() limits the accept queue. Linux caps it at net.core.somaxconn (4096 by default since Linux 5.4). The lab's settings:
net.core.somaxconn = 4096
net.ipv4.tcp_max_syn_backlog = 512
net.ipv4.tcp_abort_on_overflow = 0
Filling the accept queue
In the lab, a server called listen(2) and then waited about 7 seconds before calling accept(). Six clients connected, 200 ms apart, and each waited for a reply from the server. While the server was sleeping, its listening socket and its connections, counted by state, looked like this:
LISTEN 3 2 0.0.0.0:8080 0.0.0.0:*
3 ESTAB
1 LISTEN
The accept queue held three connections, not two. Linux considers the queue full only when it holds more than the backlog, so listen(n) admits n + 1 connections. The clients saw it differently:
3 ESTAB
3 SYN-SENT
Three clients were connected, and three were still in SYN-SENT. When the accept queue is full, Linux doesn't answer new SYNs at all; it drops them and counts them:
TcpExtListenOverflows 6 0.0
TcpExtListenDrops 6 0.0
The rejected clients kept retransmitting their SYNs, once per second at first (the linear timeouts from article 59):
1.69 s client SYN port 48158: SYN retransmission
1.88 s client SYN port 48166: SYN retransmission
2.07 s client SYN port 48168: SYN retransmission
2.71 s client SYN port 48158: SYN retransmission
2.90 s client SYN port 48166: SYN retransmission
3.10 s client SYN port 48168: SYN retransmission
3.74 s client SYN port 48158: SYN retransmission
3.92 s client SYN port 48166: SYN retransmission
4.12 s client SYN port 48168: SYN retransmission
4.76 s client SYN port 48158: SYN retransmission
4.95 s client SYN port 48166: SYN retransmission
5.14 s client SYN port 48168: SYN retransmission
5.78 s client SYN port 48158: SYN retransmission
5.97 s client SYN port 48166: SYN retransmission
6.17 s client SYN port 48168: SYN retransmission
7.83 s client SYN port 48158: SYN retransmission
8.02 s client SYN port 48166: SYN retransmission
8.21 s client SYN port 48168: SYN retransmission
And here is what each client experienced:
client 0: connect() called at 0.03 s, returned at 0.08 s, reply at 7.25 s
client 1: connect() called at 0.22 s, returned at 0.28 s, reply at 7.25 s
client 2: connect() called at 0.43 s, returned at 0.50 s, reply at 7.25 s
client 3: connect() called at 0.62 s, returned at 7.85 s, reply at 7.91 s
client 4: connect() called at 0.83 s, returned at 8.05 s, reply at 8.11 s
client 5: connect() called at 1.02 s, returned at 8.24 s, reply at 8.29 s
server: started accepting at 7.22 s
The chart below draws each client's timeline from these numbers.

Two lessons. First, a completed connect() doesn't mean a server application is handling the connection. Clients 0 to 2 were connected for 7 seconds with nobody reading their data. Second, the clients that were refused didn't get an error. They saw only a slow connect, and even after the server started accepting at 7.2 s, they had to wait for their next SYN retransmission. A server whose accept queue keeps overflowing looks to clients like random delays of whole seconds: 1, 2, 3 seconds and so on with Linux's linear SYN timeouts, or 1, 3 or 7 seconds with classic doubling.
When the accept queue is full and a client's final ACK arrives, Linux drops that ACK too, leaving the handshake in SYN-RECV and retransmitting the SYN-ACK later. Setting net.ipv4.tcp_abort_on_overflow = 1 makes it reset such connections instead, which fails clients fast but turns a short burst into errors. The default of 0 is usually better.
SYN floods
A SYN flood is a denial-of-service attack. The attacker sends SYNs, usually with forged source addresses, and never completes the handshakes. Each SYN takes a slot in the SYN queue, and the server's SYN-ACKs go to addresses that never answer. When the queue is full, legitimate clients can't get in.
In the lab, the client host sent 2,000 SYNs per second to a listen(128) server, with random source addresses from 203.0.113.0/24 (a documentation range, routed to nowhere on the lab router). Meanwhile, a legitimate client tried to connect. The test ran twice, with and without SYN cookies:
net.ipv4.tcp_syncookies = 1 (server port 80)
flood: sent 16000 spoofed SYNs in 8.0 s
legitimate client at 2.0 s: connected in 107 ms, got b'ok'
legitimate client at 2.6 s: connected in 106 ms, got b'ok'
legitimate client at 3.2 s: connected in 102 ms, got b'ok'
max SYN-RECV entries seen: 129
net.ipv4.tcp_syncookies = 0 (server port 81)
flood: sent 16000 spoofed SYNs in 8.0 s
legitimate client at 2.0 s: failed after 4.0 s: timed out
legitimate client at 6.5 s: failed after 4.0 s: timed out
legitimate client at 11.0 s: failed after 4.0 s: timed out
max SYN-RECV entries seen: 129
The chart below plots the size of the SYN queue during both runs (top) and the legitimate client's attempts (bottom).

In both runs, the SYN queue filled almost instantly and stayed full long after the flood stopped, because each entry waits through several SYN-ACK retransmissions before it expires. The difference was the legitimate client. With cookies off, its SYNs were dropped like the attacker's, and every attempt timed out, including one 2 seconds after the flood ended. With cookies on, every attempt succeeded in one round trip plus the request. The counters tell the story:
TcpExtSyncookiesSent 15809 0.0
TcpExtSyncookiesRecv 3 0.0
TcpExtTCPReqQFullDoCookies 15809 0.0
TcpExtListenDrops 15816 0.0
TcpExtTCPReqQFullDrop 15816 0.0
With cookies on, almost every SYN in the flood was answered with a cookie (SyncookiesSent), and exactly three cookies came back (SyncookiesRecv): the legitimate client's three connections. With cookies off, the same SYNs were dropped (ListenDrops).
How SYN cookies work
A SYN cookie removes the need to remember anything about a handshake. When the SYN queue is full, the server picks its ISN in a special way and then forgets the SYN completely. The ISN encodes what the server needs:
- a counter that advances once a minute, in the top 8 bits,
- an index into a small table of MSS values (536, 1300, 1440 and 1460 on Linux for IPv4), mixed into the low 24 bits,
- a cryptographic hash of the 4-tuple, the counter and a server secret, filling the rest.
When the final ACK arrives, its ACK number is the ISN plus 1. The server recomputes the hash, checks that the counter is recent (Linux accepts cookies up to 2 minutes old), recovers the MSS, and creates the connection as if it had been in the queue all along. A forged ACK would need the server's secret to pass the check.
The legitimate client's SYN-ACKs from the cookie run:
SYN-ACK at 2.1 s: ISN 1254504735 MSS 1460 wscale 7 SACK_PERM yes
SYN-ACK at 2.7 s: ISN 3777981196 MSS 1460 wscale 7 SACK_PERM yes
SYN-ACK at 3.3 s: ISN 805132298 MSS 1460 wscale 7 SACK_PERM yes
The ISNs look random, as cookies should. Cookies have costs. The MSS is rounded down to one of four values. The original SYN's window scale, SACK and ECN settings don't fit in the ISN at all. They survive only if timestamps are on, because Linux then stores them in the low bits of its timestamp value, which the client echoes back. And the SYN-ACK can't be retransmitted, because no state remains. That's why Linux's default, tcp_syncookies = 1, sends cookies only when the SYN queue overflows. A value of 2 sends them always, which is useful only for testing.
Other connection attacks
SYN floods are one of several attacks on connection management:
- Spoofed resets can end connections. RFC 5961's checks (article 62) make blind attacks much harder.
- Forged ICMP "too big" messages with a tiny MTU can force a connection to use tiny segments. Linux ignores reported MTUs below
net.ipv4.route.min_pmtu(552 bytes by default) and checks that the ICMP message quotes a segment from a real connection. - Connection exhaustion from real, completed connections that send nothing (or very slowly) ties up application resources. SYN cookies don't help here, because the attacker completes the handshake from a real address. Timeouts and per-client limits in the application do.
- Large-scale floods are handled upstream: by the provider's filtering and DDoS (distributed denial-of-service) scrubbing, and by load balancers that absorb handshakes before they reach the server.
Common questions
How many clients can connect to one server port?
As many as there are distinct 4-tuples, limited in practice by memory and file descriptors, not by ports. Each client connection differs in remote address or remote port. A server handling a million connections on port 443 is normal.
What backlog should I pass to listen()?
Big enough to absorb bursts while the application is busy: hundreds to a few thousand for a busy server. Linux caps it at net.core.somaxconn (4096 by default). A backlog doesn't reserve memory, so a generous value costs little.
Why did listen(2) accept three connections?
Linux checks whether the accept queue holds more than the backlog before refusing, so it admits backlog + 1 connections. Other systems scale the backlog differently, so treat it as a rough limit, not an exact count.
Do SYN cookies make me immune to SYN floods?
They stop the SYN queue from being the bottleneck, which is what a simple flood targets. A large enough flood can still saturate the network link or the CPU that computes cookies, and those need filtering upstream.
Why do clients sometimes connect but get no response?
Because the handshake completed in the kernel, but the application hasn't called accept() or read the data. As the lab showed, a client can be connected for seconds while the server application is busy elsewhere.
What's SO_REUSEPORT for?
It lets several sockets bind and listen on the same address and port, each with its own queues, and the kernel spreads incoming connections among them. Multi-process servers use it so each worker has its own listener (NGINX, for example, with its reuseport option).
Summary
- A listening socket matches SYNs by local address and port. Connections are separated by the full 4-tuple, so many clients share one server port.
- A specific bind accepts only connections to that address; others get a reset. An IPv6 wildcard listener also accepts IPv4 unless
IPV6_V6ONLYis set. - Linux keeps half-open handshakes in a SYN queue and completed ones in an accept queue.
listen(backlog)limits the accept queue to backlog + 1, capped bysomaxconn. - When the accept queue is full, Linux silently drops SYNs. Clients see slow connects that recover only on their next SYN retransmission, and
ListenOverflowscounts the drops. - A SYN flood fills the SYN queue with spoofed requests. With SYN cookies (Linux's default when the queue overflows), the server keeps no state and legitimate clients still connect in one round trip. Without them, the lab's legitimate client failed every attempt.
- A SYN cookie encodes a time counter, an MSS index and a keyed hash in the server's ISN, so the final ACK can be verified without stored state.
Practice questions
1. A server listens on 0.0.0.0. Two clients behind the same NAT connect from 203.0.113.7, ports 50000 and 50001. How does the server tell their segments apart?
Answer
By the remote port. The two connections are (server address, 443, 203.0.113.7, 50000) and (server address, 443, 203.0.113.7, 50001). Each segment is delivered to the connection whose full 4-tuple matches.
2. A server is bound to 127.0.0.1. A client on another host connects to the server's public address on port 5432. What does the client see?
Answer
"Connection refused". No listener matches the destination address and port, so the kernel answers the SYN with a reset, just as for a closed port.
3. A server's ss -ltn shows Recv-Q 129 Send-Q 128 on its listener, and TcpExtListenOverflows is rising. What's wrong, and what will clients notice?
Answer
The accept queue is full: the application isn't accepting connections as fast as they arrive. New SYNs are dropped, so clients see connections that take 1 or more seconds to open (waiting for SYN retransmissions) or time out. The fix is to accept faster (more workers) and possibly raise the backlog.
4. Why can't a server with SYN cookies retransmit a lost SYN-ACK?
Answer
With cookies, the server keeps no record of the SYN, so it has nothing to retransmit from. If the SYN-ACK is lost, the client's SYN retransmission produces a new SYN-ACK.
5. A legitimate client connects during a SYN flood to a server with cookies on. Which TCP features might the connection lose, and why?
Answer
The MSS is rounded down to one of four values. If timestamps are off, window scaling and SACK are also lost, because the server keeps no record of the client's SYN options and can encode them only in the timestamp. With timestamps on, they're preserved.
6. An attacker opens 10,000 real connections from a botnet and sends one byte per minute on each. Do SYN cookies help? What does?
Answer
No. The handshakes complete normally from real addresses, so the SYN queue isn't involved. The defense is in the application and load balancers: per-client connection limits, idle and request timeouts, and enough capacity to absorb many idle connections.
Review quiz
Test yourself on Part 10 (articles 57 to 63): Quiz: TCP Connections (TCP/IP Part 10)
Previous: TCP Resets: Closed Ports, Aborts, Half-Open Connections and TIME-WAIT Assassination

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.