SFML community forums

Help => Network => Topic started by: Mad Engineer on January 25, 2014, 09:19:08 pm

Title: TCPListener not able to listen to port
Post by: Mad Engineer on January 25, 2014, 09:19:08 pm
When i create listener and make him listen for incoming connection, he waits and when connection arrives he accepts connection and everything is good.

But when i set listener to non-blocking mode my console window gets spamed with error messages that listener cant listen to specific port, but when i try to connect he accepts the connection from client.

This is minimal example of my code.

#include <SFML/Network.hpp>

#include <iostream>

int main()
{
   
        sf::Clock clock;
        sf::Time deltaTime = sf::seconds(1.f / 60.f);
        sf::Time timeSinceLastUpdate = sf::Time::Zero;

        sf::TcpListener listener;
        listener.setBlocking(false);

        sf::TcpSocket sockets[3];
       
        for (int i = 0; i < 3 ; i++)
        {
                sockets[i].setBlocking(false);
        }

        int socketCounter = 0;

        while (true)
        {
                timeSinceLastUpdate += clock.restart();

                while (timeSinceLastUpdate > deltaTime)
                {
                        timeSinceLastUpdate -= deltaTime;
                       
                        //60 fps....

                        listener.listen(27100);

                        if (listener.accept(sockets[socketCounter]) == sf::Socket::Done)
                        {
                                socketCounter++;
                                std::cout << "Player Connected";
                        }

                }
               
                //max fps...
               
        }

    return 0;
}
Title: Re: TCPListener not able to listen to port
Post by: Mad Engineer on January 25, 2014, 09:36:39 pm
Never mind, solved it. I should have put listener.listen() outside of while loop so it gets called only once at the start of the program...