1
Network / Re: Help about creating server which handles many client connections
« on: December 10, 2016, 09:52:35 am »
Thanks for your answer ! That works ! I have just a little problem how to properly disconnect a client from the server ?
When I have to remove a client I do this :
I have done this in the destructor of ClientConnection:
But when I remove a client, and the I try to reconnect a new client it doesn' work it doesn't detect new client..
Here is a part of my server (inspired from the doc):
What am I doing wrong ?
Thanks you!
Clyx
When I have to remove a client I do this :
//NB : ClientConnection class is just a class that holds an id, the socket and some methods.
ClientConnection *client = m_activesConnections.at(id);
//m_activesConnections is a map<unsigned short, ClientConnection*>
delete client;
ClientConnection *client = m_activesConnections.at(id);
//m_activesConnections is a map<unsigned short, ClientConnection*>
delete client;
I have done this in the destructor of ClientConnection:
ClientConnection::~ClientConnection() {
std::cout << "Destroyed ClientConnection " << m_id << std::endl;
m_socket->disconnect();
}
std::cout << "Destroyed ClientConnection " << m_id << std::endl;
m_socket->disconnect();
}
But when I remove a client, and the I try to reconnect a new client it doesn' work it doesn't detect new client..
Here is a part of my server (inspired from the doc):
void Server::start() {
if(m_port == 0){
std::cerr << "Error the server must have a port !" << std::endl;
return;
}
if(m_listener.listen(m_port) != sf::Socket::Done){
std::cerr << "Error while starting server, port is already in use" << std::endl;
return;
}
m_selector.add(m_listener);
m_active = true;
std::cout << "Server started on port " << m_port << " waiting for new client..." << std::endl;
while(m_active){
if(m_selector.wait()){
if(m_selector.isReady(m_listener)){
sf::TcpSocket *client = new sf::TcpSocket;
if(m_listener.accept(*client) == sf::Socket::Done){
ClientConnection *clientConnection = new ClientConnection(*client);
clientConnection->setActive(true);
addClient(clientConnection);
m_selector.add(*clientConnection->getSocket());
std::cout << "new client detected, waiting for handshaking.." << std::endl;
}else{
delete client;
}
}else{
for(std::map<unsigned short, ClientConnection*>::iterator it = m_activesConnections.begin(); it != m_activesConnections.end(); it++) {
if(&it == nullptr){
std::cerr << "Error" << std::endl;
continue;
}
if(it->second != nullptr) {
sf::TcpSocket *client = it->second->getSocket();
if (m_selector.isReady(*client)) {
it->second->listenPackets();
}
}
}
}
}
}
}
if(m_port == 0){
std::cerr << "Error the server must have a port !" << std::endl;
return;
}
if(m_listener.listen(m_port) != sf::Socket::Done){
std::cerr << "Error while starting server, port is already in use" << std::endl;
return;
}
m_selector.add(m_listener);
m_active = true;
std::cout << "Server started on port " << m_port << " waiting for new client..." << std::endl;
while(m_active){
if(m_selector.wait()){
if(m_selector.isReady(m_listener)){
sf::TcpSocket *client = new sf::TcpSocket;
if(m_listener.accept(*client) == sf::Socket::Done){
ClientConnection *clientConnection = new ClientConnection(*client);
clientConnection->setActive(true);
addClient(clientConnection);
m_selector.add(*clientConnection->getSocket());
std::cout << "new client detected, waiting for handshaking.." << std::endl;
}else{
delete client;
}
}else{
for(std::map<unsigned short, ClientConnection*>::iterator it = m_activesConnections.begin(); it != m_activesConnections.end(); it++) {
if(&it == nullptr){
std::cerr << "Error" << std::endl;
continue;
}
if(it->second != nullptr) {
sf::TcpSocket *client = it->second->getSocket();
if (m_selector.isReady(*client)) {
it->second->listenPackets();
}
}
}
}
}
}
}
What am I doing wrong ?
Thanks you!
Clyx