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

Author Topic: Recuperating IP Address from selector  (Read 4340 times)

0 Members and 1 Guest are viewing this topic.

Live4Pie

  • Newbie
  • *
  • Posts: 2
    • View Profile
Recuperating IP Address from selector
« 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.
" The generation of random numbers is too important to be left to chance. " -- Someone on the internet

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Recuperating IP Address from selector
« Reply #1 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.
Want to play movies in your SFML application? Check out sfeMovie!

phear-

  • Jr. Member
  • **
  • Posts: 64
    • MSN Messenger - LOApokalypse@hotmail.com
    • View Profile
    • http://gtproductions.org
Recuperating IP Address from selector
« Reply #2 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
Eugene Alfonso
GTP | Twitter

Live4Pie

  • Newbie
  • *
  • Posts: 2
    • View Profile
Recuperating IP Address from selector
« Reply #3 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
" The generation of random numbers is too important to be left to chance. " -- Someone on the internet

 

anything