O'Reilly logo

SFML Game Development By Example by Raimondas Pupius

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing the client

With the class definition covered, it's time to actually make it do something, starting with the constructor and destructor:

Client::Client():m_listenThread(&Client::Listen, this){}
Client::~Client(){ m_socket.unbind(); }

In the client constructor's initializer list, we bind the listening thread to the Listen method of this class. Threads do not have a default empty constructor, which is why this is necessary. The destructor is simply used to unbind the socket we're using.

Now, let's take a stab at implementing the connection protocol:

bool Client::Connect(){ if (m_connected){ return false; } m_socket.bind(sf::Socket::AnyPort); sf::Packet p; StampPacket(PacketType::Connect, p); p << m_playerName; if (m_socket.send(p, m_serverIp, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required