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

Author Topic: Packets send wrong information to the server?.  (Read 3154 times)

0 Members and 1 Guest are viewing this topic.

anttton

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
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);
}
 
« Last Edit: February 07, 2017, 06:06:45 pm by anttton »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Packets send wrong information to the server?.
« Reply #1 on: February 07, 2017, 06:55:34 pm »
Quote
In the last pice of code in the server code, thats is were i get my "Error" and dont recive the packet
What does that mean exactly? Please describe your problem more precisely. What happens (or what doesn't happen)? Which status is returned? At which line of code? ...
Laurent Gomila - SFML developer

anttton

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Packets send wrong information to the server?.
« Reply #2 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Packets send wrong information to the server?.
« Reply #3 on: February 07, 2017, 08:11:40 pm »
If socket->send doesn't return sf::Socket::Done, then what does it return?
Laurent Gomila - SFML developer

anttton

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Packets send wrong information to the server?.
« Reply #4 on: February 08, 2017, 03:44:02 pm »
It returns "Error" in the console?, or have I missed a step to send the packet?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Packets send wrong information to the server?.
« Reply #5 on: February 08, 2017, 04:26:26 pm »
I mean, which value does the send function return? You test if it's different from sf::Socket::Done, but knowing what it actually is could help.
Laurent Gomila - SFML developer

 

anything