| 1 |  | 
|---|
| 2 | //////////////////////////////////////////////////////////// | 
|---|
| 3 | // Headers | 
|---|
| 4 | //////////////////////////////////////////////////////////// | 
|---|
| 5 | #include <SFML/Network.hpp> | 
|---|
| 6 | #include <iostream> | 
|---|
| 7 |  | 
|---|
| 8 |  | 
|---|
| 9 | //////////////////////////////////////////////////////////// | 
|---|
| 10 | /// Launch a server, wait for a message, send an answer. | 
|---|
| 11 | /// | 
|---|
| 12 | //////////////////////////////////////////////////////////// | 
|---|
| 13 | void runUdpServer(unsigned short port) | 
|---|
| 14 | { | 
|---|
| 15 | // Create a socket to receive a message from anyone | 
|---|
| 16 | sf::UdpSocket socket; | 
|---|
| 17 |  | 
|---|
| 18 | // Listen to messages on the specified port | 
|---|
| 19 | if (socket.bind(port) != sf::Socket::Done) | 
|---|
| 20 | return; | 
|---|
| 21 | std::cout << "Server is listening to port "<< port << ", waiting for a message... "<< std::endl; | 
|---|
| 22 |  | 
|---|
| 23 | // Wait for a message | 
|---|
| 24 | char in[128]; | 
|---|
| 25 | std::size_t received; | 
|---|
| 26 | sf::IpAddress sender; | 
|---|
| 27 | unsigned short senderPort; | 
|---|
| 28 | if (socket.receive(in, sizeof(in), received, sender, senderPort) != sf::Socket::Done) | 
|---|
| 29 | return; | 
|---|
| 30 | std::cout << "Message received from client "<< sender << ": \""<< in << "\""<< std::endl; | 
|---|
| 31 |  | 
|---|
| 32 | // Send an answer to the client | 
|---|
| 33 | const char out[] = "Hi, I'm the server"; | 
|---|
| 34 | if (socket.send(out, sizeof(out), sender, senderPort) != sf::Socket::Done) | 
|---|
| 35 | return; | 
|---|
| 36 | std::cout << "Message sent to the client: \""<< out << "\""<< std::endl; | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 |  | 
|---|
| 40 | //////////////////////////////////////////////////////////// | 
|---|
| 41 | /// Send a message to the server, wait for the answer | 
|---|
| 42 | /// | 
|---|
| 43 | //////////////////////////////////////////////////////////// | 
|---|
| 44 | void runUdpClient(unsigned short port) | 
|---|
| 45 | { | 
|---|
| 46 | // Ask for the server address | 
|---|
| 47 | sf::IpAddress server; | 
|---|
| 48 | do | 
|---|
| 49 | { | 
|---|
| 50 | std::cout << "Type the address or name of the server to connect to: "; | 
|---|
| 51 | std::cin  >> server; | 
|---|
| 52 | } | 
|---|
| 53 | while (server == sf::IpAddress::None); | 
|---|
| 54 |  | 
|---|
| 55 | // Create a socket for communicating with the server | 
|---|
| 56 | sf::UdpSocket socket; | 
|---|
| 57 |  | 
|---|
| 58 | // Send a message to the server | 
|---|
| 59 | const char out[] = "Hi, I'm a client"; | 
|---|
| 60 | if (socket.send(out, sizeof(out), server, port) != sf::Socket::Done) | 
|---|
| 61 | return; | 
|---|
| 62 | std::cout << "Message sent to the server: \""<< out << "\""<< std::endl; | 
|---|
| 63 |  | 
|---|
| 64 | // Receive an answer from anyone (but most likely from the server) | 
|---|
| 65 | char in[128]; | 
|---|
| 66 | std::size_t received; | 
|---|
| 67 | sf::IpAddress sender; | 
|---|
| 68 | unsigned short senderPort; | 
|---|
| 69 | if (socket.receive(in, sizeof(in), received, sender, senderPort) != sf::Socket::Done) | 
|---|
| 70 | return; | 
|---|
| 71 | std::cout << "Message received from "<< sender << ": \""<< in << "\""<< std::endl; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|