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

Author Topic: selector.isReady(client) returns false every time  (Read 3381 times)

0 Members and 1 Guest are viewing this topic.

zakkor

  • Newbie
  • *
  • Posts: 27
    • View Profile
selector.isReady(client) returns false every time
« 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

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!

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: selector.isReady(client) returns false every time
« Reply #1 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) { ... }

zakkor

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: selector.isReady(client) returns false every time
« Reply #2 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
« Last Edit: November 23, 2014, 04:31:23 pm by zakkor »

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: selector.isReady(client) returns false every time
« Reply #3 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.
« Last Edit: November 24, 2014, 04:16:05 pm by Gambit »

 

anything