I Have been working on some server-client code recently, which I found a very confusing problem.I have a server
listen on port and set backlog = 2, and my client create 5 thread to connect
.
In man
, I've noticed that
The backlog parameter defines the maximum length for the queue of pending connections. If a connection request arrives with the queue full, the client may receive an error with an indication of ECONNREFUSED. Alterna- tively, if the underlying protocol supports retransmission, the request may be ignored so that retries may succeed.
which means my client connect would fail or retry later.
But when my client running, it simply get a SIGPIPE signal and failed.
So I run sudo tcpdump -ilo0 port 10000
and get result:
summertekiMacBook-Pro:select summer$ sudo tcpdump -ilo0 port 10000
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on lo0, link-type NULL (BSD loopback), capture size 65535 bytes
10:29:16.396240 IP localhost.56347 > localhost.ndmp: Flags [S], seq 3366561899, win 65535, options [mss 16344,nop,wscale 4,nop,nop,TS val 396158772 ecr 0,sackOK,eol], length 0
10:29:16.396241 IP localhost.56349 > localhost.ndmp: Flags [S], seq 902832276, win 65535, options [mss 16344,nop,wscale 4,nop,nop,TS val 396158772 ecr 0,sackOK,eol], length 0
10:29:16.396242 IP localhost.56351 > localhost.ndmp: Flags [S], seq 1956535575, win 65535, options [mss 16344,nop,wscale 4,nop,nop,TS val 396158772 ecr 0,sackOK,eol], length 0
10:29:16.396244 IP localhost.56348 > localhost.ndmp: Flags [S], seq 2161003109, win 65535, options [mss 16344,nop,wscale 4,nop,nop,TS val 396158772 ecr 0,sackOK,eol], length 0
10:29:16.396246 IP localhost.56350 > localhost.ndmp: Flags [S], seq 1318035540, win 65535, options [mss 16344,nop,wscale 4,nop,nop,TS val 396158772 ecr 0,sackOK,eol], length 0
10:29:16.396296 IP localhost.ndmp > localhost.56347: Flags [S.], seq 2871094527, ack 3366561900, win 65535, options [mss 16344,nop,wscale 4,nop,nop,TS val 396158772 ecr 396158772,sackOK,eol], length 0
10:29:16.396307 IP localhost.ndmp > localhost.56351: Flags [S.], seq 3931313020, ack 1956535576, win 65535, options [mss 16344,nop,wscale 4,nop,nop,TS val 396158772 ecr 396158772,sackOK,eol], length 0
10:29:16.396332 IP localhost.ndmp > localhost.56349: Flags [S.], seq 3467781056, ack 902832277, win 65535, options [mss 16344,nop,wscale 4,nop,nop,TS val 396158772 ecr 396158772,sackOK,eol], length 0
10:29:16.396349 IP localhost.ndmp > localhost.56348: Flags [S.], seq 2666080832, ack 2161003110, win 65535, options [mss 16344,nop,wscale 4,nop,nop,TS val 396158772 ecr 396158772,sackOK,eol], length 0
10:29:16.396366 IP localhost.ndmp > localhost.56350: Flags [S.], seq 2467582351, ack 1318035541, win 65535, options [mss 16344,nop,wscale 4,nop,nop,TS val 396158772 ecr 396158772,sackOK,eol], length 0
10:29:16.396375 IP localhost.56347 > localhost.ndmp: Flags [.], ack 1, win 9186, options [nop,nop,TS val 396158772 ecr 396158772], length 0
10:29:16.396381 IP localhost.56351 > localhost.ndmp: Flags [.], ack 1, win 9186, options [nop,nop,TS val 396158772 ecr 396158772], length 0
10:29:16.396386 IP localhost.56349 > localhost.ndmp: Flags [.], ack 1, win 9186, options [nop,nop,TS val 396158772 ecr 396158772], length 0
10:29:16.396391 IP localhost.56348 > localhost.ndmp: Flags [.], ack 1, win 9186, options [nop,nop,TS val 396158772 ecr 396158772], length 0
10:29:16.396398 IP localhost.56350 > localhost.ndmp: Flags [.], ack 1, win 9186, options [nop,nop,TS val 396158772 ecr 396158772], length 0
10:29:16.396408 IP localhost.ndmp > localhost.56347: Flags [R], seq 2871094528, win 0, length 0
10:29:16.396413 IP localhost.ndmp > localhost.56351: Flags [R], seq 3931313021, win 0, length 0
10:29:16.396419 IP localhost.56347 > localhost.ndmp: Flags [P.], seq 1:1001, ack 1, win 9186, options [nop,nop,TS val 396158772 ecr 396158772], length 1000
10:29:16.396424 IP localhost.56351 > localhost.ndmp: Flags [P.], seq 1:1001, ack 1, win 9186, options [nop,nop,TS val 396158772 ecr 396158772], length 1000
10:29:16.396429 IP localhost.ndmp > localhost.56349: Flags [.], ack 1, win 9186, options [nop,nop,TS val 396158772 ecr 396158772], length 0 10:29:16.396435 IP localhost.ndmp > localhost.56348: Flags [R], seq 2666080833, win 0, length 0
10:29:16.396441 IP localhost.ndmp > localhost.56350: Flags [.], ack 1, win 9186, options [nop,nop,TS val 396158772 ecr 396158772], length 0
10:29:16.396454 IP localhost.ndmp > localhost.56347: Flags [R], seq 2871094528, win 0, length 0
10:29:16.396460 IP localhost.ndmp > localhost.56351: Flags [R], seq 3931313021, win 0, length 0
From unix network programming, connect()
will start 3-times hand shake routine, and return when server send syn && ack.
From tcpdump
output, top 10 lines told that server reply syn & sck although backlog is 2. Later, client send last-time ack and server return rst.
In my opinion, connect return with value != -1 means connection is established & client is able to send data. But log shows above shows it doesn't work that way.
So can anyone tell me which is correct?