2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Network.hpp>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Create a client and send a message to a running server
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2010-03-12 17:35:17 +08:00
|
|
|
void DoClientUDP(unsigned short port)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Ask for server address
|
2010-03-12 17:35:17 +08:00
|
|
|
sf::IpAddress serverAddress;
|
2009-01-29 00:18:34 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
std::cout << "Type address or name of the server to send the message to : ";
|
2010-03-12 17:35:17 +08:00
|
|
|
std::cin >> serverAddress;
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
2010-03-13 17:50:24 +08:00
|
|
|
while (serverAddress == sf::IpAddress::None);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Create a UDP socket for communicating with server
|
2010-03-12 17:35:17 +08:00
|
|
|
sf::SocketUDP client;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Send a message to the server
|
2010-03-12 17:35:17 +08:00
|
|
|
char message[] = "Hi, I'm a client !";
|
|
|
|
if (client.Send(message, sizeof(message), serverAddress, port) != sf::Socket::Done)
|
2009-01-29 00:18:34 +08:00
|
|
|
return;
|
2010-03-12 17:35:17 +08:00
|
|
|
std::cout << "Message sent to server : \"" << message << "\"" << std::endl;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Close the socket when we're done
|
2010-03-12 17:35:17 +08:00
|
|
|
client.Close();
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Launch a server and wait for incoming messages
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2010-03-12 17:35:17 +08:00
|
|
|
void DoServerUDP(unsigned short port)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Create a UDP socket for communicating with clients
|
2010-03-12 17:35:17 +08:00
|
|
|
sf::SocketUDP server;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Bind it to the specified port
|
2010-03-12 17:35:17 +08:00
|
|
|
if (!server.Bind(port))
|
2009-01-29 00:18:34 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Receive a message from anyone
|
2010-03-12 17:35:17 +08:00
|
|
|
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)
|
2009-01-29 00:18:34 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Display it
|
2010-03-12 17:35:17 +08:00
|
|
|
std::cout << "Message received from " << clientAddress << " on port " << clientPort
|
|
|
|
<< ": \"" << message << "\"" << std::endl;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Close the socket when we're done
|
2010-03-12 17:35:17 +08:00
|
|
|
server.Close();
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|