0

I'm trying to implement a simple example of a distributed producer consumer, and while testing, I always get the following error:

OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted

Even though many posts claim that s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) before bind would allow me to ignore the TIME_WAIT, I still get the same exception when trying to connect for a second time.

server.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 4330))
s.listen()

s.accept()
s.close()

client.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 4334))

s.connect((myaddress, 4330))
s.close()

1 Answers1

0

When client socket closed locally, if you try to connect to the same destination again, with recreated socket of same ip:port, TIME_WAIT will be in effect, regardless of SO_REUSEADDR.

To avoid this, you can make the server to initiate close() first.

Please refer this answer and this article for more details.

Liju
  • 2,273
  • 3
  • 6
  • 21