Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: selector.Wait() behavior  (Read 2990 times)

0 Members and 1 Guest are viewing this topic.

Spellbreaker

  • Newbie
  • *
  • Posts: 33
    • ICQ Messenger - 287156171
    • MSN Messenger - spellbreaker@live.de
    • Yahoo Instant Messenger - Spellbreaker1979
    • View Profile
    • http://www.apeironstudios.com
selector.Wait() behavior
« on: September 22, 2010, 11:13:27 am »
Hi again .

I have a strange behavior regarding the selector.Wait() behavior :


Code: [Select]

#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]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
selector.Wait() behavior
« Reply #1 on: September 22, 2010, 12:40:44 pm »
Can you give a complete and minimal code, including the server and the client, that I can test quickly?
Laurent Gomila - SFML developer

Spellbreaker

  • Newbie
  • *
  • Posts: 33
    • ICQ Messenger - 287156171
    • MSN Messenger - spellbreaker@live.de
    • Yahoo Instant Messenger - Spellbreaker1979
    • View Profile
    • http://www.apeironstudios.com
selector.Wait() behavior
« Reply #2 on: September 22, 2010, 12:44:43 pm »
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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
selector.Wait() behavior
« Reply #3 on: September 22, 2010, 01:32:01 pm »
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:
Code: [Select]
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.
Laurent Gomila - SFML developer

Spellbreaker

  • Newbie
  • *
  • Posts: 33
    • ICQ Messenger - 287156171
    • MSN Messenger - spellbreaker@live.de
    • Yahoo Instant Messenger - Spellbreaker1979
    • View Profile
    • http://www.apeironstudios.com
selector.Wait() behavior
« Reply #4 on: September 22, 2010, 01:40:53 pm »
Thanks :) Quite obvious now :D

Quote from: "Laurent"

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