SFML community forums

Help => Network => Topic started by: ZamenWolk on October 10, 2014, 12:22:08 am

Title: Unexpected SocketSelector behavior
Post by: ZamenWolk on October 10, 2014, 12:22:08 am
Hello everyone :)

I'm here today to seek help for my program that I've been coding for quite a bit now, and I've been stuck for about a week over a very annoying bug, and I'm here because I really don't know what to do anymore. My problem is that my socket selector always returns "false" immediatly when I call wait(), no matter if there is actually someone trying to connect on the listener, or if I put a timeout argument.I'm really out of ideas about what it could be, so any help would be more than welcome :)

EDIT : There's a much simpler version of the code 2 posts below :)

Here's my code :

main.cpp
(click to show/hide)

FightSubserver.hpp
(click to show/hide)

FightSubserver.cpp
(click to show/hide)

ServerClient.hpp
(click to show/hide)

In case you want to see the functor, here is functor.hpp
(click to show/hide)

And thank you in advance for your help :)

Zamen
Title: Re: Unexpected SocketSelector behavior
Post by: Nexus on October 10, 2014, 12:41:01 am
Please read this post (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368) carefully :)
Title: Re: Unexpected SocketSelector behavior
Post by: ZamenWolk on October 10, 2014, 12:50:42 am
Ahah yeah that's probably right, will do :) I'll come back with something better
Title: Re: Unexpected SocketSelector behavior
Post by: ZamenWolk on October 10, 2014, 01:03:05 pm
So I have updated my project os it's wayyy more simple, but there is still the error bothering me

main.cpp
bool aboutToStop = false;

int main()
{
    TcpListener             listener;
    unsigned int            subserverNumber(50);
    vector<TcpSocket*>      clientsVector;
    SocketSelector          selector;

    for (int i = 0; i < subserverNumber; i++)
    {
        TcpSocket* newSocket = new TcpSocket;
        clientsVector.push_back(newSocket);
    }

    selector.add(listener);

    //Addition of all the sockets to the selector
    for (int i = 0; i < clientsVector.size(); i++)
    {
        selector.add(*clientsVector[i]);
    }

    listener.listen(2715);

    cout << "The server host is now ready and working !" << endl;

    while (!aboutToStop)
    {
        cout << "Searching for event" << endl;

        if (selector.wait())
        {
            cout << "Found something !" << endl;
        }
        else
        {
            cout << "Couldn't find anything" << endl;
        }

        sf::sleep(sf::milliseconds(25));
    }

    return EXIT_SUCCESS;
}
Title: Re: Unexpected SocketSelector behavior
Post by: Laurent on October 10, 2014, 03:10:01 pm
The listener must be added to the selector after the call to listen(). Same for all your TcpSockets, they must be connected before being added to the selector. But honestly I have no idea what you're going to do with 50 empty sockets. Why don't you create them on the fly, on each new connection?