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

Author Topic: i found some unexpectable behavior of wait() function  (Read 3260 times)

0 Members and 1 Guest are viewing this topic.

Artyom

  • Newbie
  • *
  • Posts: 3
    • View Profile
i found some unexpectable behavior of wait() function
« on: April 17, 2018, 09:59:44 pm »
Hi everyone! i writing my client-server app and use TcpSockets with SocketSelector and thier functions wait() and isReady()
when i trying to connect on client part, it returns Socket::Status::Done and for sended messages also, but on server wait function in loop always return false by the way i checked and it didn't wait this 5 seconds: "if (selector.wait(sf::seconds(5)))". how is it possible? p.s. listener.listen(50000) returned Socket::Status::Done

here the server main loop code:

for (;;sleep(sf::milliseconds(100)))  
   {
      if (isServerClosing)
      {
         cout << "we are closing!\n";
         break;  
      }

      if (selector.wait(sf::seconds(5)))               // Make the selector wait for data on any socket
      {
         if (selector.isReady(listener))                        //if listener ready to recv new client
         {      
            TcpSocket* client = new TcpSocket;
            if (listener.accept(*client) == sf::Socket::Status::Done)      // The listener is ready to accept new client
            {
               clients.push_back(client);
               selector.add(*client);                        //container for pointers on Sockets
               cout << clients.size() << ". client connected: " << (*client).getRemoteAddress() << " and port: " << (*client).getRemotePort() << '\n';
            }
            else
            {
               // Error, we won't get a new connection, delete the socket
               cout << "error!\n";
               delete client;
            }
         }
         else                                                        //check for ready for recv sockets
         {
               for (std::vector<TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
               {
                  if (selector.isReady(**it))
                  {
                     Packet packet;                           //create packet for receiving information
                     Socket::Status status = (**it).receive(packet);
                     if (status == Socket::Status::Done)
                     {
                        packet >> msgFromClients;
                        cout << msgFromClients;         //print message on server
                        //send for clients
                        for (std::vector<TcpSocket*>::iterator it_temp = clients.begin(); it_temp != clients.end(); it_temp++)
                        {
                           if (it_temp != it)
                           {
                              if ((**it).send(packet) == Socket::Status::Disconnected)
                              {
                                 cout << "client AFK";         //delete inactive users
                                 (**it).disconnect();
                                 delete *it;
                                 clients.erase(it);
                                 selector.remove(**it);
                              }
                           }
                        }
                     }
                     else
                        if (status == Socket::Status::Disconnected)
                        {
                           cout << "client AFK";         //delete inactive users
                           (**it).disconnect();
                           delete *it;
                           clients.erase(it);
                           selector.remove(**it);
                        }
                  }
               }
         }
     
      }
   }

please, help me
« Last Edit: April 17, 2018, 10:36:56 pm by Laurent »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: i found some unexpectable behavior of wait() function
« Reply #1 on: April 19, 2018, 09:52:10 pm »
You may want to read the documentation on the functions you're using and not just assume to know what they're doing.

For example: bool sf::SocketSelector::wait(Time timeout = Time::Zero)
Quote
Wait until one or more sockets are ready to receive.

This function returns as soon as at least one socket has some data available to be received. To know which sockets are ready, use the isReady function. If you use a timeout and no socket is ready before the timeout is over, the function returns false.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Artyom

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: i found some unexpectable behavior of wait() function
« Reply #2 on: April 20, 2018, 12:30:19 am »
Ok, thanks for reply
This function returns immediately because listener can receive a connection.
But why when first client trying to connect it wrote the message of the case when we are connected and at server it don’t run any of cases(neither client connected message, nor error message), which means what isReady function returns false(this contradicts the fact that any of the sockets are ready) and continues to run the loop without waiting timeout, which means what listener can receive new connection, but when i’m trying to connect to second client, it wrote the error-message and at server it still run the loops, which are doing nothing. So, what is my problem?

Artyom

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: i found some unexpectable behavior of wait() function
« Reply #3 on: April 20, 2018, 12:33:28 am »
P.s i’m trying to run my program by local network and start at single and different computers. It gives same results

 

anything