Skip to content

Instantly share code, notes, and snippets.

@eXpl0it3r
Last active May 27, 2020 22:15
Show Gist options
  • Save eXpl0it3r/27aaa99875978d27b30b410726a6ef2d to your computer and use it in GitHub Desktop.
Save eXpl0it3r/27aaa99875978d27b30b410726a6ef2d to your computer and use it in GitHub Desktop.
SFML UDP Server & Client
#include <SFML/Network.hpp>
#include <iostream>
#include <csignal>
#include <atomic>
#include <string>
using namespace std::string_literals;
namespace
{
volatile std::atomic<bool> running = true;
void signal_handler(int)
{
running = false;
}
enum class CommunicationState
{
Send,
Receive
};
}
int main()
{
std::signal(SIGTERM, signal_handler);
const auto ServerIp = sf::IpAddress::LocalHost;
const auto ServerPort = 55000;
auto socket = sf::UdpSocket{};
std::cout << "Ready to send messages...\n";
auto state = CommunicationState::Send;
while (running)
{
if(state == CommunicationState::Send)
{
auto message = ""s;
std::getline(std::cin, message);
auto packet = sf::Packet{};
packet << message;
if (socket.send(packet, ServerIp, ServerPort) == sf::Socket::Done)
{
std::cout << "== Sending (" << ServerIp.toString() << ":" << ServerPort << ") ==\n";
std::cout << message << "\n";
state = CommunicationState::Receive;
}
else
{
std::cerr << "Error sending to " << ServerIp.toString() << ":" << ServerPort << "\n";
sf::sleep(sf::milliseconds(100));
}
}
else
{
auto packet = sf::Packet{};
auto ipAddress = sf::IpAddress{};
unsigned short port = 0;
if (socket.receive(packet, ipAddress, port) == sf::Socket::Done)
{
auto message = ""s;
packet >> message;
std::cout << "== Received (" << ipAddress.toString() << ":" << port << ") ==\n";
std::cout << message << "\n";
state = CommunicationState::Send;
}
else
{
sf::sleep(sf::milliseconds(100));
}
}
}
}
#include <SFML/Network.hpp>
#include <iostream>
#include <csignal>
#include <atomic>
#include <string>
using namespace std::string_literals;
namespace
{
volatile std::atomic<bool> running = true;
void signal_handler(int)
{
running = false;
}
}
struct Connection
{
sf::IpAddress IpAddress;
unsigned short Port;
};
int main()
{
std::signal(SIGTERM, signal_handler);
const auto RetryCount = 3;
const auto ServerIp = sf::IpAddress::Any;
const auto ServerPort = 55000;
auto socket = sf::UdpSocket{};
for (auto connectionTries = 1; socket.bind(ServerPort, ServerIp) != sf::Socket::Done; ++connectionTries)
{
if (connectionTries >= RetryCount)
{
std::cerr << "Unable to bind on port " << ServerPort << "\n";
running = false;
break;
}
}
if (running)
{
std::cout << "Binding to " << ServerIp.toString() << ":" << ServerPort << " was successful\n";
std::cout << "Waiting for client messages...\n";
}
while (running)
{
auto connection = Connection{};
auto packet = sf::Packet{};
if (socket.receive(packet, connection.IpAddress, connection.Port) == sf::Socket::Done)
{
auto data = ""s;
packet >> data;
std::cout << "== Received (" << connection.IpAddress.toString() << ":" << connection.Port << ") ==\n";
std::cout << data << "\n";
auto acknowledgePacket = sf::Packet{};
acknowledgePacket << "OK\n"s;
if (socket.send(acknowledgePacket, connection.IpAddress, connection.Port) == sf::Socket::Done)
{
std::cout << "== Send Acknowledge (" << connection.IpAddress.toString() << ":" << connection.Port << ") ==\n";
}
else
{
std::cerr << "Unable to send acknowledge (" << connection.IpAddress.toString() << ":" << connection.Port << ")\n";
}
}
else
{
sf::sleep(sf::milliseconds(100));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment