Socket.SO_REUSEADDR: Packets Received By Every Vs By Newest Listener
I got multiple processes listening on the same port subscribed to a multicast address. Packets to this address reach every process. However, when I contact them via unicast, only the newest process gets the message. Where is this behavior documented? How can I change it?
Demo program (Python):
import socket,os,struct,sys def server(): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(('', 4242)) mreq = '\xef\x01\x02\x03' + struct.pack('=I', socket.INADDR_ANY) sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) while True: d = sock.recvfrom(1024) print('[s' + str(os.getpid()) + '] ' + repr(d)) def client(): caddr = '22.214.171.124' caddr = '127.0.0.1' # Uncomment this and all servers print csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) csock.sendto('data from ' + str(os.getpid()), 0, (caddr, 4242)) def main(): if sys.argv == 's': server() else: client() if __name__ == '__main__': main()
The MSDN states that the behaviour where multiple sockets are listening to the same port for unicast messages is undefined and that there's no way to know which one will receive the data. I tested a similar setup using C++ and winsock2.2 and had similar results as when I ran your program under python (namely the process-blocking effect).
- → Do Flux (by Facebook) handle server side update with sockets
- → Running node.js on a website but not on localhost
- → How secure are socket variables in socket.io?
- → Laravel Ratchet socket Auth
- → Can I use redux-saga's es6 generators as onmessage listener for websockets or eventsource?
- → Wait for response from Async , Protractor, nodejs
- → Store session and data received with socketio
- → socket.io not emitting event from server to client on connection
- → Websockets message integrity
- → Issue with editing input via websockets by two users
- → Python websocket not getting all data
- → Unable to get the socket data through binance Websockets
- → Getting average volume for multiple coins within certain interval using sockets or REST