SFML community forums
Help => Network => Topic started by: blizter on December 09, 2011, 03:33:54 pm
-
Hello,
I'm using a TcpListener to listen to a port, I check if it worked with GetLocalPort, it return the port I chose.
Then if I open my application twice, both are listening apparently, I get no error. SFML seems to think it worked.
Obviously all the packets are only sent to the first instance that was opened.
What do you think might be causing this ?
I'm using Windows sockets.
-
What do you think might be causing this ?
Your code ;)
I tried to run the "sockets" example twice, the second one fails to listen as expected.
-
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.