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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - anttton

Pages: 1 [2]
16
Network / Re: Packets send wrong information to the server?.
« on: February 07, 2017, 07:17:15 pm »
Okay, the problem is I have another file where the game is created and when i press escape I want it to send a packets that say i have quit and want to delete the socket from the server file.

Here is a pice of code from where my game is created(the update method)
void main_multiplayergame::Update(sf::RenderWindow *window)
{
        if (this->speech->speaking)
        {
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::R) && !this->enterKey)
                {
                        this->speech->speaking = false;
                }
        }
        else
        {
                if (!this->manager->Update(window))
                {
                        return;
                }
        }

        this->Healthbar->update(window);
        this->Manabar->update(window);
        this->Expbar->update(window);


        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape))
        {

                connected = 0;
                saveSystem.x = this->manager->Get("main_guy")->getPosition().x + Entity::scroll.x;
                saveSystem.y = this->manager->Get("main_guy")->getPosition().y + Entity::scroll.y;
                saveSystem.Save();



                coreState.SetState(new main_menu());

        //////////// Here is it where i call the client function leave///////////
                client->Leave();
               
               
        }
}
 
So when i press esc I got to the main menu of the game. The leave funcktion is called from the client cpp
void Client::Leave()
{
        sf::Packet leave;
        leave << socket->Disconnected;
        socket->send(leave);
        if (socket->send(leave) != sf::Socket::Done)
        {
                std::cout << "error" << std::endl;
        }

}
 
But the problem is that this pice of code never send because in the console it appers the "Error".
Because I want so the server file remove the client when i have press esc. Currently I need to close the whole appliction to leave the server.

17
Network / Packets send wrong information to the server?.
« on: February 07, 2017, 06:04:35 pm »
Hello guys!

I was wondering why the client  are not sending the information to server when I have typed socket->send(..);
I would be glad if someone could take a look at my code and give me suggestion whats wrong.
In the last pice of code in the server code, thats is were i get my "Error" and dont recive the packet

The SERVER CODE:
std::cout << "Server Running" << std::endl;

        //se om server får några connections från någon client, bara om dom pekar man kan säga att listener håller i connectins
        sf::TcpListener listener;
       
        //tar imot connections, så flera kan ta imot connections
        sf::SocketSelector selector;
       
        bool done = false;
       
        //clienterna som ansluter sig till servern
        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())
                {
                        if (selector.isReady(listener))
                        {
                                sf::TcpSocket *socket = new sf::TcpSocket;
                                listener.accept(*socket);
                               
                                                sf::Packet ready;
                                                bool isready = true;
                                                ready << isready;
                                                socket->send(ready);
                               
                                sf::Packet packet;
                                std::string id;
                                if (socket->receive(packet) == sf::Socket::Done)
                                        packet >> id;

                                std::cout << id << " Has connected to the room" << std::endl;
                                clients.push_back(socket);
                                selector.add(*socket);

                               
                                sf::Packet leave;
                                int dis;
                                if (socket->receive(leave) == sf::Socket::Done)
                                {

                                        leave >> dis;
                                        selector.remove(*socket);


                                        //std::cout << id << " " << "has left" << std::endl;
                                        std::cout << dis << std::endl;
                                }
                        }
                       
 


And this is what i want to send through my client to the server.
void Client::Leave()
{
        sf::Packet leave;
        int dis = true;
        leave << dis;
        socket->send(leave);
}
 

Pages: 1 [2]
anything