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

Author Topic: TCPListener not able to listen to port  (Read 1810 times)

0 Members and 1 Guest are viewing this topic.

Mad Engineer

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
TCPListener not able to listen to port
« 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;
}

Mad Engineer

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: TCPListener not able to listen to port
« Reply #1 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...