2014-09-30 22:07:25 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
2024-09-24 03:55:33 +08:00
|
|
|
#include "TCP.hpp"
|
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
#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>
|
2024-02-08 21:28:57 +08:00
|
|
|
#include <optional>
|
2024-10-17 03:27:49 +08:00
|
|
|
#include <string_view>
|
2024-02-08 21:28:57 +08:00
|
|
|
|
|
|
|
#include <cstddef>
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Launch a server, wait for an incoming connection,
|
|
|
|
/// send a message and wait for the answer.
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
void runTcpServer(unsigned short port)
|
|
|
|
{
|
|
|
|
// Create a server socket to accept new connections
|
|
|
|
sf::TcpListener listener;
|
|
|
|
|
|
|
|
// Listen to the given port for incoming connections
|
2022-06-13 02:55:00 +08:00
|
|
|
if (listener.listen(port) != sf::Socket::Status::Done)
|
2014-09-30 22:07:25 +08:00
|
|
|
return;
|
|
|
|
std::cout << "Server is listening to port " << port << ", waiting for connections... " << std::endl;
|
|
|
|
|
|
|
|
// Wait for a connection
|
|
|
|
sf::TcpSocket socket;
|
2022-06-13 02:55:00 +08:00
|
|
|
if (listener.accept(socket) != sf::Socket::Status::Done)
|
2014-09-30 22:07:25 +08:00
|
|
|
return;
|
2022-06-27 18:02:35 +08:00
|
|
|
std::cout << "Client connected: " << socket.getRemoteAddress().value() << std::endl;
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Send a message to the connected client
|
2024-10-17 03:27:49 +08:00
|
|
|
static constexpr std::string_view out = "Hi, I'm the server";
|
|
|
|
if (socket.send(out.data(), out.size()) != sf::Socket::Status::Done)
|
2014-09-30 22:07:25 +08:00
|
|
|
return;
|
2024-10-17 03:27:49 +08:00
|
|
|
std::cout << "Message sent to the client: " << std::quoted(out.data()) << std::endl;
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Receive a message back from the client
|
2024-10-17 03:27:49 +08:00
|
|
|
std::array<char, 128> in{};
|
|
|
|
std::size_t received = 0;
|
|
|
|
if (socket.receive(in.data(), in.size(), received) != sf::Socket::Status::Done)
|
2014-09-30 22:07:25 +08:00
|
|
|
return;
|
2024-10-17 03:27:49 +08:00
|
|
|
std::cout << "Answer received from the client: " << std::quoted(in.data()) << std::endl;
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Create a client, connect it to a server, display the
|
|
|
|
/// welcome message and send an answer.
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
void runTcpClient(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::TcpSocket socket;
|
|
|
|
|
|
|
|
// Connect to the server
|
2022-06-13 02:55:00 +08:00
|
|
|
if (socket.connect(server.value(), port) != sf::Socket::Status::Done)
|
2014-09-30 22:07:25 +08:00
|
|
|
return;
|
2022-06-27 18:02:35 +08:00
|
|
|
std::cout << "Connected to server " << server.value() << std::endl;
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Receive a message from the server
|
2024-10-17 03:27:49 +08:00
|
|
|
std::array<char, 128> in{};
|
|
|
|
std::size_t received = 0;
|
|
|
|
if (socket.receive(in.data(), in.size(), received) != sf::Socket::Status::Done)
|
2014-09-30 22:07:25 +08:00
|
|
|
return;
|
2024-10-17 03:27:49 +08:00
|
|
|
std::cout << "Message received from the server: " << std::quoted(in.data()) << std::endl;
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Send an answer to the server
|
2024-10-17 03:27:49 +08:00
|
|
|
static constexpr std::string_view out = "Hi, I'm a client";
|
|
|
|
if (socket.send(out.data(), out.size()) != sf::Socket::Status::Done)
|
2014-09-30 22:07:25 +08:00
|
|
|
return;
|
2024-10-17 03:27:49 +08:00
|
|
|
std::cout << "Message sent to the server: " << std::quoted(out.data()) << std::endl;
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|