UPnAtom
UPnAtom copied to clipboard
Could not bind socket to multicast port
While developing and constantly running/stopping the project, sometimes there would be a bind error and thus SSDP discovery can't be started. This happens seemingly randomly. The branch used is swift-2.1.
The exact error is SSDP discovery did fail with error: Error Domain=UPnAtom Code=0 "Could not bind socket to multicast port" UserInfo={NSLocalizedDescription=Could not bind socket to multicast port}
I checked and GCDAsyncUdpSocket should set SO_REUSEADDR. Also on that development device there is no other app that is started or installed even that uses port 1900. On app termination I call stopSSDPDiscovery
. I'm not exactly sure what else can be the problem or how to recover from this without restarting the app manually.
It's only been a couple years, but in case someone is having trouble with this (like I was) here is what fixed the issue for me...
In SSDPExplorer.swift set enableReusePort to true for the GCDAsyncUdpSocket objects before the bind call, such as:
try unicastSocket?.enableReusePort(true)
try unicastSocket?.bind(toPort: 0, interface: interface)
... try multicastSocket?.enableReusePort(true) try multicastSocket?.bind(toPort: SSDPExplorer._multicastUDPPort)