SFML community forums

Help => Network => Topic started by: Live4Pie on September 08, 2009, 02:34:37 am

Title: Recuperating IP Address from selector
Post by: Live4Pie on September 08, 2009, 02:34:37 am
Hello, I am using the SelectorTCP for a basic multi-client server, and a fairly important problem arises when it's time to process the information the client sends : I cannot determine from which client the information arrives.

My question is, how would you go about recuperating the IP address from the SocketTCP?

Code: [Select]


    sf::SocketTCP Socket = Selector.GetSocketReady(i);

    if (Socket == Listener)
    {
        sf::IPAddress Address;
        sf::SocketTCP Client;
        Listener.Accept(Client, &Address);
        std::cout << "Client connected ! (" << Address << ")" << std::endl;

        //I get that the address of the Client is in Address at this point, but this doesn't solve the issue that arises when he sends information later on
        Selector.Add(Client);
    }
    else
    {

        sf::Packet Packet;
        if (Socket.Receive(Packet) == sf::Socket::Done)
        {
//This is where i'd like to know the IP of the client who just sent the message, so I can send it back to the rest of the program
            std::string Message;
            Packet >> Message;
            std::cout << "A client says : \"" << Message << "\"" << std::endl;

        }
        else
        {
            Selector.Remove(Socket);
        }
    }


Thank you for any help or advice.
Title: Recuperating IP Address from selector
Post by: Ceylo on September 08, 2009, 09:58:08 am
You must save the IP when you use sf::SocketTCP::Accept(). There is no way to get it from the socket itself.

You could use a std::map<sf::SocketTCP, sf::IPAddress> for that.
Title: Recuperating IP Address from selector
Post by: phear- on September 08, 2009, 09:15:39 pm
Build your own Socket class and derive it from sf::Socket, for both the clientside and serverside
Title: Recuperating IP Address from selector
Post by: Live4Pie on September 09, 2009, 01:15:07 am
Thank you for the rapid responses.

That was what I was thinking of doing, just making sure there is nothing I'm overlooking that would save me some time.

Again, thank you for the answers