1 | |
2 | //////////////////////////////////////////////////////////// |
3 | // Headers |
4 | //////////////////////////////////////////////////////////// |
5 | #include <SFML/Network.hpp> |
6 | #include <iostream> |
7 | |
8 | |
9 | //////////////////////////////////////////////////////////// |
10 | /// Launch a server, wait for an incoming connection, |
11 | /// send a message and wait for the answer. |
12 | /// |
13 | //////////////////////////////////////////////////////////// |
14 | void runTcpServer(unsigned short port) |
15 | { |
16 | // Create a server socket to accept new connections |
17 | sf::TcpListener listener; |
18 | |
19 | // Listen to the given port for incoming connections |
20 | if (listener.listen(port) != sf::Socket::Done) |
21 | return; |
22 | std::cout << "Server is listening to port " << port << ", waiting for connections... " << std::endl; |
23 | |
24 | // Wait for a connection |
25 | sf::TcpSocket socket; |
26 | if (listener.accept(socket) != sf::Socket::Done) |
27 | return; |
28 | std::cout << "Client connected: " << socket.getRemoteAddress() << std::endl; |
29 | |
30 | // Send a message to the connected client |
31 | const char out[] = "Hi, I'm the server" ; |
32 | if (socket.send(out, sizeof(out)) != sf::Socket::Done) |
33 | return; |
34 | std::cout << "Message sent to the client: \"" << out << "\"" << std::endl; |
35 | |
36 | // Receive a message back from the client |
37 | char in[128]; |
38 | std::size_t received; |
39 | if (socket.receive(in, sizeof(in), received) != sf::Socket::Done) |
40 | return; |
41 | std::cout << "Answer received from the client: \"" << in << "\"" << std::endl; |
42 | } |
43 | |
44 | |
45 | //////////////////////////////////////////////////////////// |
46 | /// Create a client, connect it to a server, display the |
47 | /// welcome message and send an answer. |
48 | /// |
49 | //////////////////////////////////////////////////////////// |
50 | void runTcpClient(unsigned short port) |
51 | { |
52 | // Ask for the server address |
53 | sf::IpAddress server; |
54 | do |
55 | { |
56 | std::cout << "Type the address or name of the server to connect to: " ; |
57 | std::cin >> server; |
58 | } |
59 | while (server == sf::IpAddress::None); |
60 | |
61 | // Create a socket for communicating with the server |
62 | sf::TcpSocket socket; |
63 | |
64 | // Connect to the server |
65 | if (socket.connect(server, port) != sf::Socket::Done) |
66 | return; |
67 | std::cout << "Connected to server " << server << std::endl; |
68 | |
69 | // Receive a message from the server |
70 | char in[128]; |
71 | std::size_t received; |
72 | if (socket.receive(in, sizeof(in), received) != sf::Socket::Done) |
73 | return; |
74 | std::cout << "Message received from the server: \"" << in << "\"" << std::endl; |
75 | |
76 | // Send an answer to the server |
77 | const char out[] = "Hi, I'm a client" ; |
78 | if (socket.send(out, sizeof(out)) != sf::Socket::Done) |
79 | return; |
80 | std::cout << "Message sent to the server: \"" << out << "\"" << std::endl; |
81 | } |
82 | |