1

I recently found out about SO_REUSEADDR on windows; and that tomcat uses this by default.

I did a small test to find out what happens when another process is also listening to the same port. The other process in my case was netcat. The results were surprising:

  • Irrespective of whether I start nc (netcat) first or tomcat, the response if from tomcat
  • If I stop tomcat with its bin/tomcat.bat it also kills the nc process
  • If I stop tomcat by killing the process then nc is able to respond to the next connection

What magic is this? How is tomcat always able to grab incoming request? Is this (tomcat will serve all connections even if other processes are listening) guaranteed behavior? I cannot imagine how it can be guaranteed but thought to ask.

Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
  • It's the magic of whatever Windows does when allowing processes to share TCP ports. I haven't been able to find the semantics exactly defined anywhere in MSDN, only statements that it is possible. The purpose also escapees me. – user207421 Jan 09 '14 at 20:32
  • @EJP I liked this [answer](http://stackoverflow.com/a/577905/18573) regarding its purpose. – Miserable Variable Jan 09 '14 at 20:46
  • That's the purpose of SO_REUSEADDR. It isn't the purpose of allowing multiple processes to share the same port on Windows. Unix etc. don't allow it. The point escapes me. – user207421 Jan 09 '14 at 20:56
  • @EJP You mean [this thing](http://msdn.microsoft.com/en-us/library/ms734772(v=vs.110).aspx) ? – Brian Roach Jan 09 '14 at 21:09
  • Have you read the [ServerSocket](http://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html#setReuseAddress%28boolean%29) documentation? – Elliott Frisch Jan 09 '14 at 21:17
  • @ElliottFrisch I had not read it immediately before posting the question but I have done that now. I am afraid I am not able to figure out an answer from that, can you please elaborate? – Miserable Variable Jan 09 '14 at 21:20
  • @ElliotFrisch There isn't a word about port sharing in the ServerSocket documentation. It is a Windows feature. – user207421 Jan 09 '14 at 21:33
  • @BrianRoach Thanks but I don't really see anything there that defines the semantics that apply in the OP's situation. It's a lower level thing that applies to SO_REUSEADDR on Windows. – user207421 Jan 09 '14 at 21:35

1 Answers1

2

I was able to answer this from the question that user nos asked in response to my other question.

I realized that since I was using wget http://localhost:8080 in my test, it was using IPv6 which only tomcat was listening on.

When I changed it to http://127.0.0.1:8080 I saw completely different behavior.

Community
  • 1
  • 1
Miserable Variable
  • 28,432
  • 15
  • 72
  • 133