double net_tcp_listen(unsigned short port)
{
TcpListener* sock = new TcpListener();
sock->Listen(port);
if(sock->GetLocalPort() == 0){
delete sock;
return 0;
}
return insertSock(sock);
}
How could this, running in two different process (I open my .exe twice), never return 0.
I tried MessageBox'ing the variables and indeed, GetLocalPort never returns 0.
My program calls network functions from multiple threads. I looked up just in case and Winsock is thread-safe. So I'm really confused.
I added debug messages:
Socket::Status TcpListener::Listen(unsigned short port)
{
// Create the internal socket if it doesn't exist
Create();
// Bind the socket to the specified port
sockaddr_in address = priv::SocketImpl::CreateAddress(INADDR_ANY, port);
if (bind(GetHandle(), reinterpret_cast<sockaddr*>(&address), sizeof(address)) == -1)
{
// Not likely to happen, but...
Err() << "Failed to bind listener socket to port " << port << std::endl;
MessageBox( 0, "listen error", 0, MB_ICONERROR );
return Error;
}
// Listen to the bound port
if (listen(GetHandle(), 0) == -1)
{
// Oops, socket is deaf
Err() << "Failed to listen to port " << port << std::endl;
MessageBox( 0, "listen error", 0, MB_ICONERROR );
return Error;
}
MessageBox( 0, to_string(port).c_str(), 0, MB_ICONERROR );
return Done;
}
Both process show the same message box with the same port number.