Can You Establish a TCP Connection Without listen()? Kernel Behavior Explained
This article explains that listen() merely marks a socket as passive and initializes kernel queues; without it, servers reject incoming SYN packets with RST, yet client-side connect() and TCP simultaneous open can establish connections without ever calling listen().
What Does listen() Actually Do?
Many misunderstand listen() as "blocking wait for connections", but it does three things:
Changes socket state from CLOSED to LISTEN.
Initializes two kernel queues: half-connection queue (SYN received, handshake incomplete) and full-connection queue (handshake complete, waiting for accept).
Informs TCP stack to process incoming SYN packets normally instead of replying with RST.
The actual blocking wait is accept(), not listen().
Without listen(), Server Cannot Accept Connections
When a SYN arrives, kernel searches for a socket bound to that port in LISTEN state. If none, it replies with RST, causing client connect() to fail with ECONNREFUSED.
Demonstration: bind() Without listen()
Minimal C code binding port 8888 without listen():
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
int main() {
int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
addr.sin_port = htons(8888);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(listen_fd, (struct sockaddr*)&addr, sizeof(addr));
// No listen() call
printf("Bound to port 8888, press Enter to exit
");
getchar();
close(listen_fd);
return 0;
}Running this, ss shows port bound, but nc 127.0.0.1 8888 returns "Connection refused". The socket remains in CLOSED state, so kernel sends RST.
Adding listen() Enables Connections
Adding listen(listen_fd, 128); after bind makes the same test succeed.
Scenarios Where listen() Is Not Required
1. Active Client Connection
When you act as a client calling connect(), you initiate SYN and complete the handshake without ever calling listen(). Browsers, file transfers, etc., all work this way.
2. TCP Simultaneous Open (RFC 793)
RFC 793 defines a "simultaneous open" where both ends know each other's IP and port, call connect() at the same time, exchange SYN and SYN+ACK, and complete the handshake without either socket entering LISTEN state. This is a valid protocol path but rarely used in practice.
Summary
To act as a server waiting for incoming connections, you must call listen(); otherwise the kernel replies RST to SYN.
Active clients never need listen().
TCP simultaneous open works without listen() but is practically unused.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
