86 lines
1.9 KiB
Plaintext
86 lines
1.9 KiB
Plaintext
From: jfarr at real.com (Jonothan Farr)
|
|
Date: Fri, 23 Apr 1999 12:01:50 -0700
|
|
Subject: problem with windows sockets
|
|
Message-ID: <lu3U2.5266$Bm3.336514@news20.ispnews.com>
|
|
Content-Length: 1699
|
|
X-UID: 110
|
|
|
|
I'm rather stuck trying to solve a networking problem. I've implemented a
|
|
TCP socket server derived from SocketServer.SocketServer and I want it to be
|
|
able to restart itself. The problem I'm having is binding a socket to the
|
|
same port twice. If I don't call setsockopt() with SO_REUSEADDR, then I get
|
|
a winsock error 10048, defined in winsock.h as WSAEADDRINUSE (or 48,
|
|
'Address already in use' on Linux). It seems like setting SO_REUSEADDR
|
|
should work, which it does on Linux but not on NT.
|
|
|
|
Here's some example code to illustrate my point.
|
|
|
|
---------------------
|
|
|
|
# Server:
|
|
|
|
import socket
|
|
|
|
port = 12000
|
|
queue = 5
|
|
|
|
print 'bind 1'
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
s.bind(('', port))
|
|
s.listen(queue)
|
|
client, addr = s.accept()
|
|
client.send('foo')
|
|
s.close()
|
|
|
|
print 'bind 2'
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
s.bind(('', port))
|
|
s.listen(queue)
|
|
client, addr = s.accept()
|
|
client.send('foo')
|
|
s.close()
|
|
|
|
---------------------
|
|
|
|
# Client:
|
|
|
|
import socket
|
|
|
|
host = 'localhost'
|
|
port = 12000
|
|
bufsize = 1024
|
|
|
|
print 'connect 1'
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
s.connect((host, port))
|
|
print s.recv(bufsize)
|
|
s.close()
|
|
|
|
print 'connect 2'
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
s.connect((host, port))
|
|
print s.recv(bufsize)
|
|
s.close()
|
|
|
|
---------------------
|
|
|
|
This works fine on Linux, but on NT I get a winsock error 10061 (defined as
|
|
WSAECONNREFUSED) when I try to connect a client after binding the port a
|
|
second time. Is this a problem with Winsock, a problem with Python, or a
|
|
problem with my code? How can I work around this? Any help would be
|
|
appreciated.
|
|
|
|
Thanks,
|
|
--jfarr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|