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

Author Topic: SocketSelector isReady always return false  (Read 2191 times)

0 Members and 1 Guest are viewing this topic.

Shinao

  • Newbie
  • *
  • Posts: 9
    • View Profile
SocketSelector isReady always return false
« on: April 04, 2014, 07:08:23 am »
Hi,

Here is my minimal example
./a.out foo
will launch the server
and ./a.out will launch the client
The client will send a udp message to the server which will display received.
But if we uncomment the isReady function, then we can see that it always return false.

What am I doing wrong ?
By the way, I just pulled the git repository, so I've got the last 'version'.

#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <iostream>

void    server();
void    client();

int     main(int ac, char **av)
{
  if (ac > 1)
    server();
  else
    client();
}

void    server()
{
  sf::SocketSelector _listener;
  sf::UdpSocket _socket;

  _listener.add(_socket);
  if (_socket.bind(25052) != sf::Socket::Done)
  {
    std::cout << "can't bind" << std::endl;
    return ;
  }

  char buffer[1024];
  std::size_t received = 0;
  sf::IpAddress sender;
  unsigned short port;

  _socket.setBlocking(true);
  while (1)
  {
    // if (_listener.isReady(_socket))
    // {
      _socket.receive(buffer, sizeof(buffer), received, sender, port);
      std::cout << "received" << std::endl;
    // }

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

void    client()
{
  sf::UdpSocket _socket;

  if (_socket.bind(52025) != sf::Socket::Done)
  {
    std::cout << "can't bind" << std::endl;
    return ;
  }

  _socket.send("ping", 5, sf::IpAddress::Broadcast, 25052);
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SocketSelector isReady always return false
« Reply #1 on: April 04, 2014, 07:50:22 am »
You must bind the socket before adding it to the selector. It is not really intuitive, I agree, this is due to an implementation detail.
Laurent Gomila - SFML developer

Shinao

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: SocketSelector isReady always return false
« Reply #2 on: April 04, 2014, 01:12:11 pm »
Oh, I should have thought of that.
Thanks anyway.

Edit: I tried and got the same problem. Any idea ?
« Last Edit: April 04, 2014, 01:34:50 pm by Shinao »

Shinao

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: SocketSelector isReady always return false
« Reply #3 on: April 04, 2014, 06:43:31 pm »
I allow myself to reply one more time just to be sure I'm seen.

Binding before adding to the selector doesn't seem to do the trick.
I tried on Linux with an older version of SFML and I got the same result (just to be sure it wasn't a mingw problem).

So now I'm really confused. Do you have the same result than me ?

Thanks

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SocketSelector isReady always return false
« Reply #4 on: April 04, 2014, 07:29:24 pm »
Ok, so now that this first problem is fixed I read the end of your code and... you never call _listener.wait(). So please next time, read the documentation/tutorials/examples/... carefully ;)
Laurent Gomila - SFML developer

Shinao

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: SocketSelector isReady always return false
« Reply #5 on: April 05, 2014, 05:16:15 am »
Hm... I think I had it and deleted it to have a minimal example.

Well thank you for your time !