Thanks, I've changed it to LOOPBACK, but it didn't change anything. I still see two bindings in netstat:
tcp 0 0 localhost:19080 *:* LISTEN 12273/Test
tcp 0 0 localhost:19080 *:* LISTEN 12273/Test
On Tue, 27 May 2014 10:27:58 -0700 (PDT), oxkfame@gmail.com wrote:
The following code returns 0 from the bind, even when netstat clearly shows that the port is taken by another process, ie the following function will always return true. Why?
bool isPortAvailable( int port )
{
SOCKET candidateSocket;
if(( candidateSocket = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
return false;
struct sockaddr_in serverAddress;
memset (&serverAddress, 0, sizeof (serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl( INADDR_ANY );
serverAddress.sin_port = htons( port );
if( ::bind( candidateSocket, (struct sockaddr*)&serverAddress, sizeof( serverAddress )) != 0 )
{
closesocket( candidateSocket );
return false;
}
closesocket( candidateSocket );
return true;
}
My first guess would be that the other process has the port on a
distinct IP address, your bind is to INADDR_ANY. This would let your
process have a port listening on 0.0.0.0:port which, IIRC, is distinct
from another process listening on 127.0.0.1:port for example. To prove
this guess correct, use the same IP address to which the other process
is bound and see if this function fails.