I have a third-party library that acts like an HTTP server. I pass it the address and port, which I then use to listen for incoming connections. This library listens in such a way that it does not receive exclusive use of the port and its associated port. As a result, I can listen to the same port several times.
I need to run multiple instances of this HTTP server in the same process. Each instance has a default port, but if that port is not available, it should use the next available port. This is where my problem is; I can end up using two HTTP servers listening on the same port.
I cannot change the HTTP server code, and the HTTP server will not warn me if it cannot listen to the port that I provide it, so I have to check if the port is actually used before starting each HTTP server. I tried to check if the port is already listening on by associating my own socket with SO_REUSEADDR set to FALSE and SO_EXCLUSIVEADDRUSE set to TRUE, but the bind and listen calls are both successful when an existing HTTP server is already listening on this port.
How does this HTTP server achieve this effect and how can I accurately check if the port is being listened in this way?
source
share