Hello, I have written a simple code to show the situation and also attaching console output. On the server side I just call TcpSocket::disconnect() for a client socket but SocketSelector wait always returns false but the client SocketSelector can detect the disconnection.
I am not sure this is an expected behaviour.
Thanks in advance!
Using SFML 2.6.0 official release on Windows 10 Machine
sf::TcpListener tcpListener;
sf::SocketSelector socketSelector;
sf::TcpSocket clientSocket;
std::mutex lock;
bool disconnectClientSocket = false;
void Server()
{
sf::Socket::Status tcpStatus = tcpListener.listen(LOGIN_SERVER_PORT);
if (tcpStatus != sf::Socket::Done)
{
std::cout << "[Server] TcpListener couldn't be initialized!" << std::endl;
return;
}
else
{
std::cout << "[Server] TcpListener initialized!" << std::endl;
}
sf::Socket::Status acceptStatus = tcpListener.accept(clientSocket);
if (acceptStatus == sf::Socket::Done)
{
std::cout << "[Server] A new client is accepted!" << std::endl;
socketSelector.add(clientSocket);
std::cout << "[Server] Starting to listen with socket selector!" << std::endl;
while (true)
{
lock.lock();
if (socketSelector.wait(sf::milliseconds(1000)))
{
std::cout << "[Server] socketSelector.wait() returns true" << std::endl;
if (socketSelector.isReady(clientSocket))
{
std::cout << "[Server] socketSelector.isReady(clientSocket) returns true" << std::endl;
}
else
{
std::cout << "[Server] socketSelector.isReady(clientSocket) returns false" << std::endl;
}
}
else
{
std::cout << "[Server] socketSelector.wait() returns false" << std::endl;
}
lock.unlock();
Sleep(100);
if (disconnectClientSocket)
{
std::cout << "[Server] Disconnecting client socket" << std::endl;
clientSocket.disconnect();
disconnectClientSocket = false;
}
}
}
}
void Client()
{
sf::SocketSelector selector;
sf::TcpSocket tcpSocket;
sf::Socket::Status status = tcpSocket.connect(GSO_SERVER_IP, LOGIN_SERVER_PORT, sf::milliseconds(1000));
if (status != sf::Socket::Done)
{
std::cout << "[Client] Connection to server failed!" << std::endl;
return;
}
else
{
std::cout << "[Client] Connected to server!" << std::endl;
}
selector.add(tcpSocket);
bool waiting = true;
while (waiting)
{
if (selector.wait())
{
std::cout << "[Client] SocketSelector returns true!" << std::endl;
if (selector.isReady(tcpSocket))
{
std::cout << "[Client] SocketSelector tcpSocket is ready!" << std::endl;
waiting = false;
}
else
{
std::cout << "[Client] SocketSelector tcpSocket is not ready!" << std::endl;
}
}
else
{
std::cout << "[Client] SocketSelector returns false!" << std::endl;
}
Sleep(100);
}
}
int main()
{
std::thread s(Server);
Sleep(1000);
std::thread c(Client);
while (true)
{
int i;
std::cin >> i;
if(i == 0)
{
lock.lock();
std::cout << "[MainThread] Lock is locked. Sending disconnect command!" << std::endl;
disconnectClientSocket = true;
lock.unlock();
}
}
return 0;
}
[Server] TcpListener initialized!
[Server] A new client is accepted!
[Server] Starting to listen with socket selector!
[Client] Connected to server!
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false
0
[Server] socketSelector.wait() returns false
[MainThread] Lock is locked. Sending disconnect command!
[Server] Disconnecting client socket
[Server] socketSelector.wait() returns false
[Client] SocketSelector returns true!
[Client] SocketSelector tcpSocket is ready!
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false