1
Network / Network several connections TCP server
« on: February 12, 2017, 09:03:47 am »
Hello guys!
When i connect to my server with two computers it works great. But when i disconnect with both computers, nothing happens. I have debugged the server and still can se two clients are connect but I have left the "game / program" with both computers. I have try to solve what the problem is but i cant fix it.
Ill be happy if someone can give me suggestion what the problem is.
This is my code from the server
When i connect to my server with two computers it works great. But when i disconnect with both computers, nothing happens. I have debugged the server and still can se two clients are connect but I have left the "game / program" with both computers. I have try to solve what the problem is but i cant fix it.
Ill be happy if someone can give me suggestion what the problem is.
This is my code from the server
int main()
{
std::cout << "Server Running" << std::endl;
sf::TcpListener listener;
sf::SocketSelector selector;
bool done = false;
std::string connected;
std::string id;
std::vector<sf::TcpSocket*> clients;
int Connected;
unsigned short port;
std::cout << "Type the portnumber:";
std::cin >> port;
//listenern hör till denna port alltså ervern
listener.listen(port);
selector.add(listener);
while (!done)
{
if (selector.wait(sf::Time::Zero))
{
if (selector.isReady(listener))
{
sf::TcpSocket *client = new sf::TcpSocket;
listener.accept(*client);
std::cout << "client has connected" << std::endl;
clients.push_back(client);
selector.add(*client);
}
else
{
for (std::vector<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end();)
{
sf::TcpSocket &client = **it;
if (selector.isReady(client))
{
// The client has sent some data, we can receive it
sf::Packet packet;
if (client.receive(packet) == sf::Socket::Done)
{
PacketTypeBool type;
packet >> type >> id;
if (type == SendStringID)
{
std::cout << id << " has connected" << std::endl;
}
std::cout << clients.size() << std::endl;
}
if (client.receive(packet) == sf::Socket::Disconnected)
{
std::cout << id << " disconnected" << std::endl;
std::cout << "list size: " << clients.size();
selector.remove(client);
client.disconnect();
delete(&client);
it = clients.erase(it);
std::cout << " then: " << clients.size() << std::endl;
}
else
{
++it;
}
}
}
}
}
}
system("Pause");
return 0;
}
{
std::cout << "Server Running" << std::endl;
sf::TcpListener listener;
sf::SocketSelector selector;
bool done = false;
std::string connected;
std::string id;
std::vector<sf::TcpSocket*> clients;
int Connected;
unsigned short port;
std::cout << "Type the portnumber:";
std::cin >> port;
//listenern hör till denna port alltså ervern
listener.listen(port);
selector.add(listener);
while (!done)
{
if (selector.wait(sf::Time::Zero))
{
if (selector.isReady(listener))
{
sf::TcpSocket *client = new sf::TcpSocket;
listener.accept(*client);
std::cout << "client has connected" << std::endl;
clients.push_back(client);
selector.add(*client);
}
else
{
for (std::vector<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end();)
{
sf::TcpSocket &client = **it;
if (selector.isReady(client))
{
// The client has sent some data, we can receive it
sf::Packet packet;
if (client.receive(packet) == sf::Socket::Done)
{
PacketTypeBool type;
packet >> type >> id;
if (type == SendStringID)
{
std::cout << id << " has connected" << std::endl;
}
std::cout << clients.size() << std::endl;
}
if (client.receive(packet) == sf::Socket::Disconnected)
{
std::cout << id << " disconnected" << std::endl;
std::cout << "list size: " << clients.size();
selector.remove(client);
client.disconnect();
delete(&client);
it = clients.erase(it);
std::cout << " then: " << clients.size() << std::endl;
}
else
{
++it;
}
}
}
}
}
}
system("Pause");
return 0;
}