0

running on Mac with two jetty instances i've arrived to this peculiar situation:

~$ lsof -ni :9905
COMMAND   PID    USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
java    40320 *******  394u  IPv6 0xf5e143eb9100c205      0t0  TCP *:9905 (LISTEN)
java    40325 *******  302u  IPv6 0xf5e143eb79be9005      0t0  TCP 127.0.0.1:9905 (LISTEN)

I've tried to recreate this situation in python, but that failed (as i feel it should):

>>> s = socket.socket()
>>> s.bind(("",12345))
>>> s.listen(1)
>>> z = socket.socket()
>>> z.bind(("localhost",12345))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 48] Address already in use

I also tried with AF_INET6 with the same results.

does anyone have a clue on what's going on here? how can this even occur? I was under the impression that the asterisk is bound to ALL addresses... if this is indeed a normal behaviour, what is the correct why to recreate it in python?

Thanks!

Nadav
  • 3
  • 2

1 Answers1

1

This is how to reproduce it on osx. No idea why it works (don't tell anybody, I try to pose as a linux guy), on linux it doesn't.

See https://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t for detailed explanations on sockets

>>> import socket
>>> s=socket.socket()
>>> s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
>>> s.bind(("",12345))
>>> s.listen(1)
>>> z=socket.socket()
>>> z.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
>>> z.bind(("127.0.0.1",12345))
>>> z.listen(1)

And the lsof

Python    7501 danb    3u  IPv4 0xac5d4de7add842f      0t0  TCP *:12345 (LISTEN)
Python    7501 danb    4u  IPv4 0xac5d4de7bb7928f      0t0  TCP 127.0.0.1:12345 (LISTEN)
Dan
  • 661
  • 4
  • 7
  • thanks for the answer and the great link which was essentially what I was looking for. I looked at jetty's code and indeed it uses SO_REUSEADDR. – Nadav Feb 05 '15 at 12:53