1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Network.hpp>
#include <iostream>
////////////////////////////////////////////////////////////
/// Create a client and send a message to a running server
///
////////////////////////////////////////////////////////////
void DoClientUDP(unsigned short Port)
{
// Ask for server address
sf::IPAddress ServerAddress;
do
{
std::cout << "Type address or name of the server to send the message to : ";
std::cin >> ServerAddress;
}
while (!ServerAddress.IsValid());
// Create a UDP socket for communicating with server
sf::SocketUDP Client;
// Send a message to the server
char Message[] = "Hi, I'm a client !";
if (Client.Send(Message, sizeof(Message), ServerAddress, Port) != sf::Socket::Done)
return;
std::cout << "Message sent to server : \"" << Message << "\"" << std::endl;
// Close the socket when we're done
Client.Close();
}
////////////////////////////////////////////////////////////
/// Launch a server and wait for incoming messages
///
////////////////////////////////////////////////////////////
void DoServerUDP(unsigned short Port)
{
// Create a UDP socket for communicating with clients
sf::SocketUDP Server;
// Bind it to the specified port
if (!Server.Bind(Port))
return;
// Receive a message from anyone
sf::IPAddress ClientAddress;
unsigned short ClientPort;
char Message[128];
std::size_t Received;
if (Server.Receive(Message, sizeof(Message), Received, ClientAddress, ClientPort) != sf::Socket::Done)
return;
// Display it
std::cout << "Message received from " << ClientAddress << " on port " << ClientPort
<< ": \"" << Message << "\"" << std::endl;
// Close the socket when we're done
Server.Close();
}
|