SFML community forums

Help => Network => Topic started by: zakkor on November 23, 2014, 03:38:50 pm

Title: selector.isReady(client) returns false every time
Post by: zakkor on November 23, 2014, 03:38:50 pm
Hi there! I followed the usage example from here: http://sfml-dev.org/documentation/2.0/classsf_1_1SocketSelector.php (http://sfml-dev.org/documentation/2.0/classsf_1_1SocketSelector.php)

Here is the server-side code:
(click to show/hide)

And the client-side code:
(click to show/hide)

I run the server, then run the client, it connects successfully, but when I try to send the packet from the client to the server, it gets sent but not received by the server, which means getting bombarded with "selector no worko", signifying that selector.isReady(client) is always false, even when data is sent to one of the clients.

Any ideas?

PS: Tell me if i forgot to provide anything essential!
Title: Re: selector.isReady(client) returns false every time
Post by: Jesper Juhl on November 23, 2014, 04:24:12 pm
I don't see you ever calling selector.wait()
If you never wait for data it will never be ready.
Also, your for loop could be written much simpler as simply
for (auto &client : clients) { ... }
Title: Re: selector.isReady(client) returns false every time
Post by: zakkor on November 23, 2014, 04:27:07 pm
Thanks! I added selector.wait(), and now it works. But there's a problem!
I call this code every frame, and now it's blocking input. What can I do?

edit:
I removed selector.wait() and the check for selector.isReady() completely, and then set the client as non-blocking, and it seems to work!
Please tell me if what I'm doing is bad, lol
Title: Re: selector.isReady(client) returns false every time
Post by: Gambit on November 23, 2014, 07:39:00 pm
You should use a timeout for selector.wait, say 100us (the length of calling winapi sleep()).

while (running) {
   // maybe you have some stuff before this
   
   if (oSelector.wait(sf::microseconds(100))) {
      if (oSelector.isReady(oListener)) {
         auto upoSocket = std::make_unique<sf::TcpSocket>();
         upoSocket->setBlocking(false);
         
         if (oListener.accept(*upoSocket) == sf::Socket::Status::Done) {
            // store this for later user
         }
      }
      else {
         for (auto& iii : all_you_sockets) {
            if (oSelector.isReady(iii)) {
               // this socket is ready, receive data
            }
         }
      }
   }
   
   // render and update things
}
 

Non-blocking sockets plus a small timeout on selector.wait will give you a seamless execution. Ofcourse, the more sockets you have and the more stuff you do with each socket, the slow your program may become but it shouldnt be  too noticible.