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

Author Topic: Tcp Selector  (Read 2842 times)

0 Members and 1 Guest are viewing this topic.

JeromeBaur

  • Newbie
  • *
  • Posts: 2
    • View Profile
Tcp Selector
« on: October 20, 2011, 04:15:04 pm »
Hey guys

I use SFML 2.0 Network because the 1.6 version of network doesn't work by me. But SFML 2.0 has only 1 example of socket. I want to make a selector for a online Game. I used the old example from version 1.6 and changed the code for example SocketTcp to TcpSocket. Now i have a problem. In SFML 1.6 i can compare a Listener with a Socket but in 2.0 that doesn't work.  :cry:


Can someone give me a example how i can use selectors in 2.0?

Here's the code of the serverfunction:

Code: [Select]
void DoServer(unsigned short Port)
{
    // Create a socket for listening to incoming connections
    sf::TcpListener Listener;
    std::cout << "Listening to port " << Port << ", waiting for connections..." << std::endl;

    // Create a selector for handling several sockets (the listener + the socket associated to each client)
    sf::SocketSelector Selector;

    // Add the listener
    Selector.Add(Listener);

    // Loop while... we close the program :)
    std::vector<sf::TcpSocket> SocketsReady;
    while (true)
    {
        // Get the sockets ready for reading
        //Selector.IsReady(SocketsReady);

        // We can read from each returned socket
for (std::vector<sf::TcpSocket>::iterator i = SocketsReady.begin(); i != SocketsReady.end(); ++i)
        {
            // Get the current socket
            sf::TcpSocket Socket = *i;

if (Listener == Socket )
            {
                // If the listening socket is ready, it means that we can accept a new connection
                sf::IpAddress Address;
                sf::TcpSocket Client;
                Listener.Accept(Socket);
                std::cout << "Client connected ! (" << Address << ")" << std::endl;

                // Add it to the selector
                Selector.Add(Client);
            }
            else
            {
                // Else, it is a client socket so we can read the data he sent
                sf::Packet Packet;
                if (Socket.Receive(Packet) == sf::Socket::Done)
                {
                    // Extract the message and display it
                    std::string Message;
                    Packet >> Message;
                    std::cout << "A client says : \"" << Message << "\"" << std::endl;
                }
            }
        }
    }
}


I hope you can help me.

Thx a lot =)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Tcp Selector
« Reply #1 on: October 20, 2011, 04:26:31 pm »
The online documentation of sf::SocketSelector has a complete code example.
Laurent Gomila - SFML developer