1
Network / Problem with SocketSelector
« on: May 20, 2015, 03:22:43 pm »
Well i get interesting results
code:
i just can't understand why after connection and some time it ignores if and don't allow any more connections
code:
sf::TcpListener Listener;
//Listener.setBlocking(false);
if (Listener.listen(CFG.SelfHostPort) != sf::Socket::Done)
{
std::cout << "Cannont listen on port....\n";
}
std::list<sf::TcpSocket*> clients;
sf::SocketSelector Selector;
Selector.add(Listener);
std::cout << "started server init\n";
while (AppRunning)
{
sf::sleep(sf::milliseconds(1.0f));
std::cout << "."; //after init prints one dot, after connection and some time it starts prinitng a lot of dots
if (Selector.wait(sf::Time::Zero))
{
std::cout << "test\n";
if (Selector.isReady(Listener))
{
std::cout << "test2\n";
// The listener is ready: there is a pending connection
sf::TcpSocket* client = new sf::TcpSocket;
if (Listener.accept(*client) == sf::Socket::Done)
{
std::cout << "new client!\n";
// 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;
std::cout << "deleting client\n";
}
}
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
size_t Size = 0;
char Buffer[1024];
if (client.receive(Buffer, sizeof(Buffer), Size) == sf::Socket::Done)
{
std::cout << "done receiving\n";
}
std::string ReceivedStr;
ReceivedStr.append(Buffer, Buffer + Size);
if (ReceivedStr.empty())
{
client.disconnect();
Selector.remove(client);
break;
}
else
ParseConnection(ReceivedStr, client);
}
}
}
}
}
}
//Listener.setBlocking(false);
if (Listener.listen(CFG.SelfHostPort) != sf::Socket::Done)
{
std::cout << "Cannont listen on port....\n";
}
std::list<sf::TcpSocket*> clients;
sf::SocketSelector Selector;
Selector.add(Listener);
std::cout << "started server init\n";
while (AppRunning)
{
sf::sleep(sf::milliseconds(1.0f));
std::cout << "."; //after init prints one dot, after connection and some time it starts prinitng a lot of dots
if (Selector.wait(sf::Time::Zero))
{
std::cout << "test\n";
if (Selector.isReady(Listener))
{
std::cout << "test2\n";
// The listener is ready: there is a pending connection
sf::TcpSocket* client = new sf::TcpSocket;
if (Listener.accept(*client) == sf::Socket::Done)
{
std::cout << "new client!\n";
// 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;
std::cout << "deleting client\n";
}
}
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
size_t Size = 0;
char Buffer[1024];
if (client.receive(Buffer, sizeof(Buffer), Size) == sf::Socket::Done)
{
std::cout << "done receiving\n";
}
std::string ReceivedStr;
ReceivedStr.append(Buffer, Buffer + Size);
if (ReceivedStr.empty())
{
client.disconnect();
Selector.remove(client);
break;
}
else
ParseConnection(ReceivedStr, client);
}
}
}
}
}
}
i just can't understand why after connection and some time it ignores if and don't allow any more connections