4

Disinterested curiosity...

In Java I listen on a socket, with backlog of 1:

ServerSocket ss = new ServerSocket(4000, 1);

In shells I run

netcat localhost 4000

many times - 5 so far.

The connections are never rejected. Every instance of netcat sits and waits until my ServerSocket is destroyed.

Backlog length is 1 - that means it should only let one incoming connection queue up, and then reject, does it not? ((I don't know if the queue includes the first one - not important right now.))

I know I can make this work by closing the ServerSocket (and then opening another one when I'm ready), but... shouldn't it work anyway?

Have I misunderstood?

Mark Smith
  • 880
  • 1
  • 8
  • 25
  • 1
    http://stackoverflow.com/a/20353156/3166303 – leeor Oct 17 '15 at 17:59
  • 1
    This behaviour is platform-dependent. Windows issues an RST when the backlog fills up, which results in 'connection refused'. Unix, Linux just drop the SYN packet. – leeor Oct 17 '15 at 18:00
  • If you're running on linux, read this http://veithen.github.io/2014/01/01/how-tcp-backlog-works-in-linux.html . netcat might timeout after a few minutes if you send something on it, otherwise it'll think the connection is established. (or try `echo 1 > /proc/sys/net/ipv4/tcp_abort_on_overflow`) – nos Oct 17 '15 at 18:08
  • Thanks. I'd accept one of those if it were an answer :-) – Mark Smith Oct 17 '15 at 18:26

1 Answers1

2

As I wrote here, quoted above,

This behaviour is platform-dependent. Windows issues an RST when the backlog fills up, which results in 'connection refused'. Unix, Linux just drop the SYN packet.

NB Backlog length isn't 1. The platform can adjust it up or down. The smallest minimum backlog length in history was five, in early BSD releases. It is now fifty or even five hundred on some platforms.

Community
  • 1
  • 1
user207421
  • 305,947
  • 44
  • 307
  • 483