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