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

Author Topic: Sending char from client to server one time. The server receives it two times.  (Read 668 times)

0 Members and 1 Guest are viewing this topic.

Static

  • Newbie
  • *
  • Posts: 2
    • View Profile
Hi!

I'm new on the forum, and have some experience on SFML. What a great library, thank you devs!

I have a problem. I'm trying to send a char from the client to the server over socket. I'm doing this via a key press. When I press a key from the client one time the server receives the key press (char) two times. What's wrong? Is it something to do with the thread? I have tried to figure this out, but I have no brains enough... ;D

client:
    while (window.isOpen()) {
        sf::Event ev;
        while (window.pollEvent(ev)) {
            // Window closure
            if (ev.type == sf::Event::Closed ||
                (ev.type == sf::Event::KeyPressed &&
                    ev.key.code == sf::Keyboard::Escape)) {
                window.close();
            }

            if (ev.type == sf::Event::KeyPressed) {
                if (ev.key.code == sf::Keyboard::Right) {
                                         
                    char keyPressed = 'A';
                    if (socket.send(&keyPressed, sizeof(keyPressed)) == sf::Socket::Done)
                        std::cout << "sending A to server ";

                                 
                }
            }
        }

engine.cpp (server)
void Engine::startServer()
{

        port = 5000;
        client.setBlocking(true);
        std::cout << "Server started...\n";
        if (listener.listen(port) != sf::Socket::Status::Done)
                return;

        std::cout << "- Server is listening to port " << port << std::endl;

        networkThread = std::thread(&Engine::processIncomingData, this);

}

void Engine::processIncomingData(){

while (running) {
        char receivedKeyCode;
        size_t received;

        // Accept client connection and setup...


        sf::Socket::Status status = listener.accept(client);

        if (status == sf::Socket::Done) {
                std::cout << "Client connected: " << client.getRemoteAddress() << std::endl;

                while (running) { // Loop to process key press data

                       

                        sf::Socket::Status receiveStatus = client.receive(&receivedKeyCode, sizeof(receivedKeyCode), received);

                        if (receiveStatus == sf::Socket::Done) {
                                std::cout << "Received key code from client: " << receivedKeyCode << std::endl;

                                // Lock the mutex before accessing shared resources
                                std::lock_guard<std::mutex> lock(callbackMutex);

                                sf::Keyboard::Key pressedKey = static_cast<sf::Keyboard::Key>(receivedKeyCode);

                                if (receivedKeyCode == &#39;A&#39;) {
                                       
                                     

                                                        pressForward();

                                               
                                        }
                               

                        }

                        else if (receiveStatus == sf::Socket::Disconnected) {
                                std::cout << "Client disconnected: " << client.getRemoteAddress() << std::endl;

                                break;
                        }


                }
        }

}


}