You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems it is possible to start two Datagram servers in the same script on the same port. I thought that should not be possible. If I do the same for a StreamServer I get an error as expected (socket.error: [Errno 98] Address already in use: ('127.0.0.1', 8000)).
The second started server can receive data, the first does not get anything. After stopping the second, no data is received at all.
If a normal UDP Server is already started, I get an exception (as expected).
What I've run:
fromgevent.serverimportDatagramServerfromgeventimportsleep, spawn, TimeoutimportrandomimportargparseimportsocketimportsysclassUDPServer(object):
def__init__(self, host):
self.udp_server=DatagramServer(host, handle=self.handler)
self.udp_server.start()
def__repr__(self):
return"<UDPServer id={} started: {}>".format(id(self),
self.udp_server.started)
defhandler(self, data, address):
print"{}: got data {} from {}".format(self, data, address)
defstop(self):
self.udp_server.close()
defsender(host, timeout=1):
sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print"start sending data to", hostwithTimeout(timeout, exception=False):
while(True):
rand=random.random()
print"send", randsock.sendto(str(rand), host)
sleep(.1)
print"sender finished"defplain_datagram_server(host, port, time):
sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the portprint"start plain UDP server for {}s".format(time)
server_address= (host, port)
print'starting up on %s port %s'%server_addresssock.bind(server_address)
print"started plain datagram server"sleep(time)
print"stop plain datagram server"deftest_datagram_server(host):
print"DatagramServer test on", hostprint"Start udp1 on {} and send data".format(host)
udp1=UDPServer(host)
udp2=Noneprint"udp1", udp1print"udp2", udp2sender_greenlet=spawn(sender, host)
sender_greenlet.join()
print"\nStart udp2 on {} and send data".format(host)
udp2=UDPServer(host)
print"udp1", udp1print"udp2", udp2sender_greenlet=spawn(sender, host)
sender_greenlet.join()
print"\nStop udp2 and send data"udp2.stop()
print"udp1", udp1print"udp2", udp2sender_greenlet=spawn(sender, host)
sender_greenlet.join()
if__name__=='__main__':
parser=argparse.ArgumentParser()
parser.add_argument("mode", choices=["send", "test", "test_plain",
"receive_plain"])
parser.add_argument("--time", default=10)
parser.add_argument("--port", default=8000)
parser.add_argument("--host", default="localhost")
args=parser.parse_args()
ifargs.mode=="send":
sender((args.host, args.port), args.time)
elifargs.mode=="test":
test_datagram_server((args.host, args.port))
elifargs.mode=="test_plain":
spawn(plain_datagram_server, args.host, args.port, args.time)
sleep()
test_datagram_server((args.host, args.port))
elifargs.mode=="receive_plain":
plain_datagram_server(args.host, args.port, args.time)
If you run my script with mode "test", it shows the described behaviour. If you run it with mode "test_plain", a normal UDP Server is started on a socket and then the DatagramServer fails.
The text was updated successfully, but these errors were encountered:
I believe you're seeing the effects of SO_REUSEADDR, which is set to True by default. Its effects are highly system dependent (and maybe even family---INET vs DGRAM) dependent. It seems to make no difference at all on OS X (I can't "double bind" ever), while on Linux 4.4.21 I can unless I set reuse_addr to False.
This is poorly documented, perhaps because the effects vary so widely. But you can change it for your particular system:
Description:
It seems it is possible to start two Datagram servers in the same script on the same port. I thought that should not be possible. If I do the same for a StreamServer I get an error as expected (socket.error: [Errno 98] Address already in use: ('127.0.0.1', 8000)).
The second started server can receive data, the first does not get anything. After stopping the second, no data is received at all.
If a normal UDP Server is already started, I get an exception (as expected).
What I've run:
If you run my script with mode "test", it shows the described behaviour. If you run it with mode "test_plain", a normal UDP Server is started on a socket and then the DatagramServer fails.
The text was updated successfully, but these errors were encountered: