2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Network.hpp>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Create a client and connect it to a running server
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-07-12 06:17:24 +08:00
|
|
|
void DoClientTCP(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 connect to : ";
|
2009-07-12 06:17:24 +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 TCP socket for communicating with server
|
2009-07-12 06:17:24 +08:00
|
|
|
sf::SocketTCP client;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Connect to the specified server
|
2009-07-12 06:17:24 +08:00
|
|
|
if (client.Connect(port, serverAddress) != sf::Socket::Done)
|
2009-01-29 00:18:34 +08:00
|
|
|
return;
|
2009-07-12 06:17:24 +08:00
|
|
|
std::cout << "Connected to server " << serverAddress << std::endl;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Receive a message from the client
|
2009-07-12 06:17:24 +08:00
|
|
|
char message[128];
|
|
|
|
std::size_t received;
|
|
|
|
if (client.Receive(message, sizeof(message), received) != sf::Socket::Done)
|
2009-01-29 00:18:34 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Show it
|
2009-07-12 06:17:24 +08:00
|
|
|
std::cout << "Message received from server : \"" << message << "\"" << std::endl;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Define a message to send back to the server
|
2009-07-12 06:17:24 +08:00
|
|
|
char toSend[] = "Hi, I'm a client !";
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Send the message
|
2009-07-12 06:17:24 +08:00
|
|
|
if (client.Send(toSend, sizeof(toSend)) != sf::Socket::Done)
|
2009-01-29 00:18:34 +08:00
|
|
|
return;
|
2009-07-12 06:17:24 +08:00
|
|
|
std::cout << "Message sent to server : \"" << toSend << "\"" << std::endl;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Close the socket when we're done
|
2009-07-12 06:17:24 +08:00
|
|
|
client.Close();
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Launch a server and wait for incoming connections
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-07-12 06:17:24 +08:00
|
|
|
void DoServerTCP(unsigned short port)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Create a TCP socket for communicating with clients
|
2009-07-12 06:17:24 +08:00
|
|
|
sf::SocketTCP server;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Listen to a port for incoming connections
|
2009-07-12 06:17:24 +08:00
|
|
|
if (!server.Listen(port))
|
2009-01-29 00:18:34 +08:00
|
|
|
return;
|
2009-07-12 06:17:24 +08:00
|
|
|
std::cout << "Server is listening to port " << port << ", waiting for connections... " << std::endl;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Wait for a connection
|
2010-03-12 17:35:17 +08:00
|
|
|
sf::IpAddress clientAddress;
|
2009-07-12 06:17:24 +08:00
|
|
|
sf::SocketTCP client;
|
|
|
|
if (server.Accept(client, &clientAddress) != sf::Socket::Done)
|
2009-01-29 00:18:34 +08:00
|
|
|
return;
|
2009-07-12 06:17:24 +08:00
|
|
|
std::cout << "Client connected : " << clientAddress << std::endl;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Send a message to the client
|
2009-07-12 06:17:24 +08:00
|
|
|
char toSend[] = "Hi, I'm the server";
|
|
|
|
if (client.Send(toSend, sizeof(toSend)) != sf::Socket::Done)
|
2009-01-29 00:18:34 +08:00
|
|
|
return;
|
2009-07-12 06:17:24 +08:00
|
|
|
std::cout << "Message sent to the client : \"" << toSend << "\"" << std::endl;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Receive a message back from the client
|
2009-07-12 06:17:24 +08:00
|
|
|
char message[128];
|
|
|
|
std::size_t received;
|
|
|
|
if (client.Receive(message, sizeof(message), received) != sf::Socket::Done)
|
2009-01-29 00:18:34 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Show the message
|
2009-07-12 06:17:24 +08:00
|
|
|
std::cout << "Message received from the client : \"" << message << "\"" << std::endl;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Close the sockets when we're done
|
2009-07-12 06:17:24 +08:00
|
|
|
client.Close();
|
|
|
|
server.Close();
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|