Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Listen always returns Done  (Read 2328 times)

0 Members and 1 Guest are viewing this topic.

blizter

  • Newbie
  • *
  • Posts: 7
    • View Profile
Listen always returns Done
« 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Listen always returns Done
« Reply #1 on: December 09, 2011, 08:48:06 pm »
Quote
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.
Laurent Gomila - SFML developer

blizter

  • Newbie
  • *
  • Posts: 7
    • View Profile
Listen always returns Done
« Reply #2 on: December 10, 2011, 06:17:18 pm »
Code: [Select]
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:

Code: [Select]
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.