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

A simple chat application

We have the basic framework in place to handle connections, so let's build something with it! How about a neat little console-based chat program? Let's start with the server by creating a separate project and a new file called Server_Main.cpp. The first thing we're going to need is a packet handler:

void Handler(sf::IpAddress& l_ip, const PortNumber& l_port, const PacketID& l_id, sf::Packet& l_packet, Server* l_server) { ClientID id = l_server->GetClientID(l_ip, l_port); if (id >= 0){ if ((PacketType)l_id == PacketType::Disconnect){ l_server->RemoveClient(l_ip, l_port); sf::Packet p; StampPacket(PacketType::Message, p); std::string message; message = "Client left! " + l_ip.toString() + ":" + std::to_string(l_port); p << ...

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