2014-09-30 22:07:25 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Network.hpp>
|
2022-07-05 00:20:58 +08:00
|
|
|
|
2022-03-15 11:00:20 +08:00
|
|
|
#include <iomanip>
|
2014-09-30 22:07:25 +08:00
|
|
|
#include <iostream>
|
2022-06-27 18:02:35 +08:00
|
|
|
#include <optional>
|
2014-09-30 22:07:25 +08:00
|
|
|
|
2024-02-08 21:28:57 +08:00
|
|
|
#include <cstddef>
|
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// 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
|
2022-06-13 02:55:00 +08:00
|
|
|
if (socket.bind(port) != sf::Socket::Status::Done)
|
2014-09-30 22:07:25 +08:00
|
|
|
return;
|
|
|
|
std::cout << "Server is listening to port " << port << ", waiting for a message... " << std::endl;
|
|
|
|
|
|
|
|
// Wait for a message
|
2022-07-05 00:20:58 +08:00
|
|
|
char in[128];
|
|
|
|
std::size_t received;
|
2022-06-27 18:02:35 +08:00
|
|
|
std::optional<sf::IpAddress> sender;
|
2022-07-05 00:20:58 +08:00
|
|
|
unsigned short senderPort;
|
2022-06-13 02:55:00 +08:00
|
|
|
if (socket.receive(in, sizeof(in), received, sender, senderPort) != sf::Socket::Status::Done)
|
2014-09-30 22:07:25 +08:00
|
|
|
return;
|
2022-06-27 18:02:35 +08:00
|
|
|
std::cout << "Message received from client " << sender.value() << ": " << std::quoted(in) << std::endl;
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Send an answer to the client
|
|
|
|
const char out[] = "Hi, I'm the server";
|
2022-06-13 02:55:00 +08:00
|
|
|
if (socket.send(out, sizeof(out), sender.value(), senderPort) != sf::Socket::Status::Done)
|
2014-09-30 22:07:25 +08:00
|
|
|
return;
|
2022-03-15 11:00:20 +08:00
|
|
|
std::cout << "Message sent to the client: " << std::quoted(out) << std::endl;
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Send a message to the server, wait for the answer
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
void runUdpClient(unsigned short port)
|
|
|
|
{
|
|
|
|
// Ask for the server address
|
2022-06-27 18:02:35 +08:00
|
|
|
std::optional<sf::IpAddress> server;
|
2014-09-30 22:07:25 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
std::cout << "Type the address or name of the server to connect to: ";
|
2022-07-05 00:20:58 +08:00
|
|
|
std::cin >> server;
|
|
|
|
} while (!server.has_value());
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Create a socket for communicating with the server
|
|
|
|
sf::UdpSocket socket;
|
|
|
|
|
|
|
|
// Send a message to the server
|
|
|
|
const char out[] = "Hi, I'm a client";
|
2022-06-13 02:55:00 +08:00
|
|
|
if (socket.send(out, sizeof(out), server.value(), port) != sf::Socket::Status::Done)
|
2014-09-30 22:07:25 +08:00
|
|
|
return;
|
2022-03-15 11:00:20 +08:00
|
|
|
std::cout << "Message sent to the server: " << std::quoted(out) << std::endl;
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Receive an answer from anyone (but most likely from the server)
|
2022-07-05 00:20:58 +08:00
|
|
|
char in[128];
|
|
|
|
std::size_t received;
|
2022-06-27 18:02:35 +08:00
|
|
|
std::optional<sf::IpAddress> sender;
|
2022-07-05 00:20:58 +08:00
|
|
|
unsigned short senderPort;
|
2022-06-13 02:55:00 +08:00
|
|
|
if (socket.receive(in, sizeof(in), received, sender, senderPort) != sf::Socket::Status::Done)
|
2014-09-30 22:07:25 +08:00
|
|
|
return;
|
2022-06-27 18:02:35 +08:00
|
|
|
std::cout << "Message received from " << sender.value() << ": " << std::quoted(in) << std::endl;
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|