views:

335

answers:

2

I want to be able to reuse some ports, and that's why I'm using setsockopt on my sockets, with the following code:

sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)

However, this doesn't really work. I'm not getting a bind error either, but the server socket just isn't responding (it seems to start , but if I try to connect to it, it doesn't enter the select loop). This behaviour appears if the script ended unexpectedly, and if I change the port the server is listening on, everything works again. Can you provide some advice?

EDIT: I renamed the socket to sock. It was just a name I chose for this code snippet.

+1  A: 

setsockopt is a method of a socket object. module socket doesn't have a setsockopt attribute.

SilentGhost
I named my socket object in this example socket. In my code, it's name is "listening_sock"
Geo
+1  A: 

It appears that SO_REUSEADDR has different semantics on Windows vs Unix.

See this msdn article (particularly the chart below "Using SO_EXCLUSIVEADDRUSE") and this unix faq.

Also, see this python bug discussion, this twisted bug discussion, and this list of differences between Windows and Unix sockets.

paleozogt