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

Author Topic: Selector.wait() not calling after client disconnect  (Read 6010 times)

0 Members and 2 Guests are viewing this topic.

FleshyOverlord

  • Newbie
  • *
  • Posts: 22
    • View Profile
Selector.wait() not calling after client disconnect
« on: May 26, 2019, 01:46:25 am »
I have been working on a TCP networking game recently and have been having problems with the
 sf::SocketSelector::wait()
function. Whenever a client disconnects the selector basically stops functioning, it will not receive any packets or even trigger after a new client tries to connect. Is this intentional as a part of the SFML engine, or is this most likely a programming error on my part?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Selector.wait() not calling after client disconnect
« Reply #1 on: May 26, 2019, 12:46:07 pm »
If you're using SFML 2.4.x make sure to update to SFML 2.5.x (or git master). We have fixed some issue with reusing of sockets.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FleshyOverlord

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Selector.wait() not calling after client disconnect
« Reply #2 on: May 26, 2019, 04:04:58 pm »
I just checked the version I was using and found it was SFML 2.5.1

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Selector.wait() not calling after client disconnect
« Reply #3 on: May 26, 2019, 05:54:34 pm »
Can you provide a minimal example that reproduces the issue?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FleshyOverlord

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Selector.wait() not calling after client disconnect
« Reply #4 on: May 27, 2019, 07:23:32 am »
I believe the problem is in my code. I was able to create a quick client connection test that worked perfectly even when clients disconnected. To test it, you should only need this file.

#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <vector>
#include <iostream>
#include <sstream>

int main(){
bool isClient;
std::cout << "Client?" << std::endl;
std::string answer;
std::cin >> answer;
if(answer == "yes"){
    isClient = true;
}
else{
    isClient = false;
}
// Create a socket to listen to new connections
sf::TcpListener listener;
if(!isClient){
listener.listen(55001);
std::cout << "setup server" << std::endl;
}
bool running = true;
//socket used by client
sf::TcpSocket mSocket;
// Create a list to store the future clients
std::list<sf::TcpSocket*> clients;
// Create a selector
sf::SocketSelector selector;
// Add the listener to the selector
selector.add(listener);
// Endless loop that waits for new connections
if(isClient){
    if(mSocket.connect(sf::IpAddress("127.0.0.1"), 55001) == sf::Socket::Status::Done){
    std::cout << "connected" << std::endl;
    }
}

while (running)
{
    // Make the selector wait for data on any socket
    if (selector.wait())
    {
        std::cout << "receiving data" << std::endl;
       if(!isClient){
        // Test the listener
        if (selector.isReady(listener))
        {
            std::cout << "added client" << std::endl;
            // The listener is ready: there is a pending connection
            sf::TcpSocket* client = new sf::TcpSocket;
            if (listener.accept(*client) == sf::Socket::Done)
            {
                // Add the new client to the clients list
                clients.push_back(client);
                // Add the new client to the selector so that we will
                // be notified when he sends something
                selector.add(*client);
            }
            else
            {
                // Error, we won't get a new connection, delete the socket
                delete client;
            }
        }
        else
        {
            // The listener socket is not ready, test all other sockets (the clients)
            for (std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
            {
                sf::TcpSocket& client = **it;
                if (selector.isReady(client))
                {
                    // The client has sent some data, we can receive it
                    sf::Packet packet;
                    sf::Socket::Status stat = client.receive(packet);
                    if (stat == sf::Socket::Done)
                    {
                    }
                    else if (stat == sf::Socket::Status::Disconnected){
                        std::cout << "disconnected" << std::endl;
                        selector.remove(**it);
                        it = clients.erase(it);
                    }
                }
            }
        }
    }
    else{

    }
    }
}
return 0;
}

 
« Last Edit: May 27, 2019, 07:25:29 am by FleshyOverlord »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Selector.wait() not calling after client disconnect
« Reply #5 on: May 27, 2019, 08:45:54 am »
So does this code reproduce the issue or is it the code that works perfectly? :D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FleshyOverlord

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Selector.wait() not calling after client disconnect
« Reply #6 on: May 27, 2019, 11:27:42 pm »
This code does work however I am still getting errors on my side but I think I found the root of the problem. Thank you for the quick responses!

FleshyOverlord

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Selector.wait() not calling after client disconnect
« Reply #7 on: May 28, 2019, 06:36:47 pm »
I found the root of the problem is that the
selector.wait(sf::seconds(3)
is returning false without waiting 3 seconds after a client disconnects. What does the returning false mean?

FleshyOverlord

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Selector.wait() not calling after client disconnect
« Reply #8 on: May 29, 2019, 01:00:09 am »
Thank you for your help eXpl0it3r! I found that in a different file I was deleting the socket before I called
selector.remove(socket)
which is what caused the errors.

 

anything