I believe the problem is in my code. I was able to create a quick client connection test that worked perfectly even when clients disconnected. To test it, you should only need this file.
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <vector>
#include <iostream>
#include <sstream>
int main(){
bool isClient;
std::cout << "Client?" << std::endl;
std::string answer;
std::cin >> answer;
if(answer == "yes"){
isClient = true;
}
else{
isClient = false;
}
// Create a socket to listen to new connections
sf::TcpListener listener;
if(!isClient){
listener.listen(55001);
std::cout << "setup server" << std::endl;
}
bool running = true;
//socket used by client
sf::TcpSocket mSocket;
// Create a list to store the future clients
std::list<sf::TcpSocket*> clients;
// Create a selector
sf::SocketSelector selector;
// Add the listener to the selector
selector.add(listener);
// Endless loop that waits for new connections
if(isClient){
if(mSocket.connect(sf::IpAddress("127.0.0.1"), 55001) == sf::Socket::Status::Done){
std::cout << "connected" << std::endl;
}
}
while (running)
{
// Make the selector wait for data on any socket
if (selector.wait())
{
std::cout << "receiving data" << std::endl;
if(!isClient){
// Test the listener
if (selector.isReady(listener))
{
std::cout << "added client" << std::endl;
// The listener is ready: there is a pending connection
sf::TcpSocket* client = new sf::TcpSocket;
if (listener.accept(*client) == sf::Socket::Done)
{
// Add the new client to the clients list
clients.push_back(client);
// Add the new client to the selector so that we will
// be notified when he sends something
selector.add(*client);
}
else
{
// Error, we won't get a new connection, delete the socket
delete client;
}
}
else
{
// The listener socket is not ready, test all other sockets (the clients)
for (std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
{
sf::TcpSocket& client = **it;
if (selector.isReady(client))
{
// The client has sent some data, we can receive it
sf::Packet packet;
sf::Socket::Status stat = client.receive(packet);
if (stat == sf::Socket::Done)
{
}
else if (stat == sf::Socket::Status::Disconnected){
std::cout << "disconnected" << std::endl;
selector.remove(**it);
it = clients.erase(it);
}
}
}
}
}
else{
}
}
}
return 0;
}