79 lines
2.8 KiB
C++
Raw Normal View History

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
2024-09-23 13:55:33 -06:00
#include "UDP.hpp"
#include <SFML/Network.hpp>
2022-07-04 11:20:58 -05:00
2022-03-14 21:00:20 -06:00
#include <iomanip>
#include <iostream>
2022-06-27 12:02:35 +02:00
#include <optional>
2024-10-16 13:27:49 -06:00
#include <string_view>
2024-02-08 14:28:57 +01:00
#include <cstddef>
////////////////////////////////////////////////////////////
/// Launch a server, wait for a message, send an answer.
///
////////////////////////////////////////////////////////////
void runUdpServer(unsigned short port)
{
// Create a socket to receive a message from anyone
sf::UdpSocket socket;
// Listen to messages on the specified port
if (socket.bind(port) != sf::Socket::Status::Done)
return;
std::cout << "Server is listening to port " << port << ", waiting for a message... " << std::endl;
// Wait for a message
2024-10-16 13:27:49 -06:00
std::array<char, 128> in{};
2024-04-27 06:44:27 +00:00
std::size_t received = 0;
2022-06-27 12:02:35 +02:00
std::optional<sf::IpAddress> sender;
2024-04-27 06:44:27 +00:00
unsigned short senderPort = 0;
2024-10-16 13:27:49 -06:00
if (socket.receive(in.data(), in.size(), received, sender, senderPort) != sf::Socket::Status::Done)
return;
2024-10-16 13:27:49 -06:00
std::cout << "Message received from client " << sender.value() << ": " << std::quoted(in.data()) << std::endl;
// Send an answer to the client
2024-10-16 13:27:49 -06:00
static constexpr std::string_view out = "Hi, I'm the server";
if (socket.send(out.data(), out.size(), sender.value(), senderPort) != sf::Socket::Status::Done)
return;
2024-10-16 13:27:49 -06:00
std::cout << "Message sent to the client: " << std::quoted(out.data()) << std::endl;
}
////////////////////////////////////////////////////////////
/// Send a message to the server, wait for the answer
///
////////////////////////////////////////////////////////////
void runUdpClient(unsigned short port)
{
// Ask for the server address
2022-06-27 12:02:35 +02:00
std::optional<sf::IpAddress> server;
do
{
std::cout << "Type the address or name of the server to connect to: ";
2022-07-04 11:20:58 -05:00
std::cin >> server;
} while (!server.has_value());
// Create a socket for communicating with the server
sf::UdpSocket socket;
// Send a message to the server
2024-10-16 13:27:49 -06:00
static constexpr std::string_view out = "Hi, I'm a client";
if (socket.send(out.data(), out.size(), server.value(), port) != sf::Socket::Status::Done)
return;
2024-10-16 13:27:49 -06:00
std::cout << "Message sent to the server: " << std::quoted(out.data()) << std::endl;
// Receive an answer from anyone (but most likely from the server)
2024-10-16 13:27:49 -06:00
std::array<char, 128> in{};
2024-04-27 06:44:27 +00:00
std::size_t received = 0;
2022-06-27 12:02:35 +02:00
std::optional<sf::IpAddress> sender;
2024-04-27 06:44:27 +00:00
unsigned short senderPort = 0;
2024-10-16 13:27:49 -06:00
if (socket.receive(in.data(), in.size(), received, sender, senderPort) != sf::Socket::Status::Done)
return;
2024-10-16 13:27:49 -06:00
std::cout << "Message received from " << sender.value() << ": " << std::quoted(in.data()) << std::endl;
}