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

Author Topic: Network several connections TCP server  (Read 3443 times)

0 Members and 1 Guest are viewing this topic.

denkoo

  • Newbie
  • *
  • Posts: 1
    • View Profile
Network several connections TCP server
« on: February 12, 2017, 09:03:47 am »
Hello guys!

When i connect to my server with two computers it works great. But when i disconnect with both computers, nothing happens. I have debugged the server and still can se two clients are connect but I have left the "game / program" with both computers. I have try to solve what the problem is but i cant fix it.

Ill be happy if someone can give me suggestion what the problem is.

This is my code from the server

int main()
{
        std::cout << "Server Running" << std::endl;

        sf::TcpListener listener;

       
        sf::SocketSelector selector;

        bool done = false;
       
       
        std::string connected;
        std::string id;
       
       
        std::vector<sf::TcpSocket*> clients;
        int Connected;
        unsigned short port;
       
std::cout << "Type the portnumber:";
        std::cin >> port;
       
        //listenern hör till denna port alltså ervern
        listener.listen(port);


        selector.add(listener);


        while (!done)
        {
                if (selector.wait(sf::Time::Zero))
                {
                        if (selector.isReady(listener))
                        {
                                sf::TcpSocket *client = new sf::TcpSocket;
                                listener.accept(*client);
                               
std::cout << "client has connected" << std::endl;
                                        clients.push_back(client);
                                        selector.add(*client);
                               
                               
                        }
                        else
                        {
                                for (std::vector<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end();)
                                {
                                        sf::TcpSocket &client = **it;
                                        if (selector.isReady(client))
                                        {
                                                // The client has sent some data, we can receive it
                                                sf::Packet packet;
                                                if (client.receive(packet) == sf::Socket::Done)
                                                {
                                                       
                                                        PacketTypeBool type;
                                                        packet >> type >> id;
                                                        if (type == SendStringID)
                                                        {
                                                                std::cout << id << " has connected" << std::endl;

                                                        }
                                                        std::cout << clients.size() << std::endl;
                                                }

                                                if (client.receive(packet) == sf::Socket::Disconnected)
                                                {
                                                        std::cout << id << " disconnected" << std::endl;
                                                        std::cout << "list size: " << clients.size();
                                                        selector.remove(client);
                                                        client.disconnect();
                                                        delete(&client);
                                                        it = clients.erase(it);


                                                        std::cout << " then: " << clients.size() << std::endl;
                                                }
                                                else
                                                {
                                                        ++it;
                                                }

                                        }
                                }
                        }
                }
        }

        system("Pause");
        return 0;
}

 
« Last Edit: February 12, 2017, 07:01:29 pm by denkoo »

Godsend72

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Network several connections TCP server
« Reply #1 on: February 15, 2017, 02:23:16 pm »
You can make a send disconnect function on your client side, where you send the disconnect code. When the server receives it, it deletes that client. You would call it in a destructor or when closing your game.

Other way would be to create a ping function, where you ping the server from the client. Then create a timeout variable for each client on your server side, where you track the how long has it been since last ping. If it was more that x time, you delete that client.

Or you could ping the client from the server, and if the status of the send ping function is sf::Socket::Disconnected, just delete the client.

There are probably more ways, I am using the first and the second one.
The second one is also useful if you want to display the ping on the client game.

 

anything