SFML community forums
Help => Network => Topic started by: Spellbreaker on September 22, 2010, 11:13:27 am
-
Hi again .
I have a strange behavior regarding the selector.Wait() behavior :
#include "server.hpp"
cClient::cClient(sf::TcpSocket *socket, sf::Uint32 UID) {
this->socket = socket;
this->UID = UID;
}
cClient::~cClient() {
this->socket->Disconnect();
delete this->socket;
}
sf::Uint32 cClient::getUID() {
return this->UID;
}
int main() {
unsigned short Port = 1337;
sf::SocketSelector selector;
sf::TcpListener listener;
//std::list<sf::TcpSocket*> clients;
std::list<cClient> clientList;
listener.Listen(Port);
selector.Add(listener);
sf::Uint32 UID;
std::cout << "Server is listening to Port " << Port << ", waiting for connections..." << std::endl;
while (true) {
if (selector.Wait()) {
std::cout << "#" << std::endl;
if (selector.IsReady(listener)) {
std::cout << "b" << std::endl;
UID++;
sf::TcpSocket* client = new sf::TcpSocket;
if (listener.Accept(*client) == sf::Socket::Done) {
cClient newClient(client, UID);
// clients.push_back(client);
clientList.push_back(newClient);
selector.Add(*client);
std::cout << "New Client connected!" << std::endl;
}
} else {
std::cout << "or what?" << std::endl;
for ( tClientList::iterator it = clientList.begin(); it != clientList.end(); ++it ) {
sf::TcpSocket& client = *it->socket;
if ( selector.IsReady(client) ) {
sf::Packet packet;
if (client.Receive(packet) == sf::Socket::Done) {
std::string Message;
packet >> Message;
if ( Message == "qq" ) {
selector.Remove(client);
client.Disconnect();
std::cout << "Socket closed." << std::endl;
} else {
std::cout << "A client says : \"" << Message << "\"" << std::endl;
}
} else {
std::cout << "Socket Killed" << std::endl;
selector.Remove(client);
}
}
}
}
}
}
}
For some reason, the Wait() only waits until the first client is connected, after that the cout "#" never appears again, and the loop is looping endlessly (of course ).
What am I doing wrong?
Usign SFML 2 rev 1570
greets,
Spellbreajer[/code]
-
Can you give a complete and minimal code, including the server and the client, that I can test quickly?
-
Sure.
Here's an archive with the Source, just two files ( client.cpp and server.cpp ) with project files ( c::b ) and stuff.
http://www.apeironstudios.com/nettest.7z
-
The problem is your cClient destructor, which destroys the socket. But as you store copies of cClient instances in your list, the originals are destroyed and the socket they share with their copy are destroyed as well. Which leaves the copies stored in the list with invalid pointers to sockets.
You should rather do that:
class cClient : sf::NonCopyable // to avoid this kind of problems in the future
{
...
sf::Socket socket; // no more pointer
};
std::list<cClient*> clients; // store by pointer in the list
By the way, if you'd given me a true minimal code (ie. with no cClient class), you'd have found the error by yourself... ;)
When people here ask for minimal codes, they are not asking for pieces of your original code, but instead they want the poster to find the actual piece of code which is really causing the problem; the solution often comes naturally after doing that.
-
Thanks :) Quite obvious now :D
By the way, if you'd given me a true minimal code (ie. with no cClient class), you'd have found the error by yourself... ;)
When people here ask for minimal codes, they are not asking for pieces of your original code, but instead they want the poster to find the actual piece of code which is really causing the problem; the solution often comes naturally after doing that.
Okay, will keep that in Mind for the next Time :D