When i connect with my client it works great and in the console for the server it pop up that I have joined. When I then try to join the server from my other computer nothing happens, and in the console for the server nothing pops up that I have joined.
I have also try to first join with my other computer first and it works great but then when I try to join with my other computer nothing happens.
The server seems just only can take one TCP Connection.
I need some help to solve this problem!
int main()
{
std::cout << "Server Running" << std::endl;
//se om server får några connections från någon client, bara om dom pekar man kan säga att listener håller i connectins
sf::TcpListener listener;
//tar imot connections, så flera kan ta imot connections
sf::SocketSelector selector;
bool done = false;
std::string connected;
std::string id;
//clienterna som ansluter sig till servern
std::vector<sf::TcpSocket*> clients;
int Connected;
unsignedshort 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;
if (listener.accept(*client) == sf::Socket::Done)
{
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;
}