What I want to do is for the client to detect server disconnection and reconnect automatically when it starts listening again.
To test that, I start the client, I start the server then I stop it and try to start it again and see if the client reconnects.
The problem is that the server cannot rebind to the same port again.
What should I do ?
Client:
void SDP::RunNet()
{
sf::TcpSocket CMDSocket;
CMDSocket.setBlocking(false);
sf::Time CMDSocketTimeout = sf::seconds(0.05f);
std::cout<<"Connecting to CMD... "<<std::flush;
mutexRunning.lock();
bool stillRunning = Running;
mutexRunning.unlock();
while(stillRunning)
{
if(!connectedToCMD)
{
if(CMDSocket.connect("localhost", 55001,CMDSocketTimeout)==sf::Socket::Done)
{
connectedToCMD=true;
std::cout<<"Connected to CMD."<<std::endl;
}
sf::sleep(CMDSocketTimeout);
}
else
{
sf::Packet packet;
sf::Socket::Status CMDSocketStatus = CMDSocket.receive(packet);
if(CMDSocketStatus==sf::Socket::Disconnected)
{
connectedToCMD=false;
CMDSocket.disconnect();
std::cout<<"CMD disconnected."<<std::endl<<"Trying to reconnect... "<<std::flush;
}
}
mutexRunning.lock();
stillRunning = Running;
mutexRunning.unlock();
}
if(connectedToCMD)
{
CMDSocket.disconnect();
std::cout<<"Disonnected from CMD."<<std::endl;
}
Server:
void CMD::RunNet()
{
sf::TcpListener listener;
listener.setBlocking(false);
listener.listen(55001);
sf::SocketSelector selector;
selector.add(listener);
std::list<sf::TcpSocket*> clients;
mutexRunning.lock();
bool stillRunning = Running;
mutexRunning.unlock();
while (stillRunning)
{
if (selector.wait(sf::seconds(0.015f)))
{
if (selector.isReady(listener))
{
sf::TcpSocket* client = new sf::TcpSocket;
if (listener.accept(*client) == sf::Socket::Done)
{
std::cout<<"client accepted: "<<client->getRemoteAddress()<<std::endl;
std::cout<<"list size: "<<clients.size();
clients.push_back(client);
std::cout<<" then: "<<clients.size()<<std::endl;
selector.add(*client);
}
else
{
delete client;
}
}
else
{
for (std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
{
sf::TcpSocket& client = **it;
if (selector.isReady(client))
{
sf::Packet packet;
sf::Socket::Status clientStatus = client.receive(packet);
if (clientStatus == sf::Socket::Done)
{
std::cout<<"Done"<<std::endl;
}
if (clientStatus == sf::Socket::Disconnected)
{
std::cout<<"Client disconnected"<<std::endl;
std::cout<<"list size: "<<clients.size();
selector.remove(client);
client.disconnect();
delete(&client);
clients.erase(it);
it--;
std::cout<<" then: "<<clients.size()<<std::endl;
}
}
}
}
}
mutexRunning.lock();
stillRunning = Running;
mutexRunning.unlock();
}
std::cout<<"Disconnecting from all clients."<<std::endl;
for (std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
{
sf::TcpSocket& client = **it;
std::cout<<"list size: "<<clients.size();
selector.remove(client);
client.disconnect();
delete(&client);
clients.erase(it);
it--;
std::cout<<" then: "<<clients.size()<<std::endl;
}
clients.clear();
selector.remove(listener);
listener.close();
}