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.

Geo
  • 93,257
  • 117
  • 344
  • 520

2 Answers2

3

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
  • 6,393
  • 11
  • 51
  • 94
1

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

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • I named my socket object in this example socket. In my code, it's name is "listening_sock" – Geo Apr 28 '09 at 10:25