UPnP/UDP/Multicast Port Sharing
Posted: Wed May 02, 2018 8:47 am
If your plugin needs to bind to a specific port to listen for UPnP and/or multicast, be sure to set the appropriate socket options so that others that may need to also listen on that port can share it with you. Here's a very simplified code snippit that illustrates it:
Specifically, the socket.SO_REUSEPORT option will allow others to also bind to the port. The OS will then just send each broadcast packet to each listening process.
- Code: Select all
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.bind(('', 1900))
Specifically, the socket.SO_REUSEPORT option will allow others to also bind to the port. The OS will then just send each broadcast packet to each listening process.