FS#86 - Rewrite the sockets API

Updated the API documentation of the whole network module
The system headers are no longer included by the sfml-network public headers

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1475 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
LaurentGom 2010-03-23 09:39:43 +00:00
parent a09ee0f9e3
commit 9280771665
66 changed files with 3976 additions and 2992 deletions

View file

@ -7,87 +7,75 @@
////////////////////////////////////////////////////////////
/// Create a client and connect it to a running server
/// Launch a server, wait for an incoming connection,
/// send a message and wait for the answer.
///
////////////////////////////////////////////////////////////
void DoClientTCP(unsigned short port)
void RunTcpServer(unsigned short port)
{
// Ask for server address
sf::IpAddress serverAddress;
do
{
std::cout << "Type address or name of the server to connect to : ";
std::cin >> serverAddress;
}
while (serverAddress == sf::IpAddress::None);
// Create a server socket to accept new connections
sf::TcpListener listener;
// Create a TCP socket for communicating with server
sf::SocketTCP client;
// Connect to the specified server
if (client.Connect(port, serverAddress) != sf::Socket::Done)
return;
std::cout << "Connected to server " << serverAddress << std::endl;
// Receive a message from the client
char message[128];
std::size_t received;
if (client.Receive(message, sizeof(message), received) != sf::Socket::Done)
return;
// Show it
std::cout << "Message received from server : \"" << message << "\"" << std::endl;
// Define a message to send back to the server
char toSend[] = "Hi, I'm a client !";
// Send the message
if (client.Send(toSend, sizeof(toSend)) != sf::Socket::Done)
return;
std::cout << "Message sent to server : \"" << toSend << "\"" << std::endl;
// Close the socket when we're done
client.Close();
}
////////////////////////////////////////////////////////////
/// Launch a server and wait for incoming connections
///
////////////////////////////////////////////////////////////
void DoServerTCP(unsigned short port)
{
// Create a TCP socket for communicating with clients
sf::SocketTCP server;
// Listen to a port for incoming connections
if (!server.Listen(port))
// Listen to the given port for incoming connections
if (listener.Listen(port) != sf::Socket::Done)
return;
std::cout << "Server is listening to port " << port << ", waiting for connections... " << std::endl;
// Wait for a connection
sf::IpAddress clientAddress;
sf::SocketTCP client;
if (server.Accept(client, &clientAddress) != sf::Socket::Done)
sf::TcpSocket socket;
if (listener.Accept(socket) != sf::Socket::Done)
return;
std::cout << "Client connected : " << clientAddress << std::endl;
std::cout << "Client connected: " << socket.GetRemoteAddress() << std::endl;
// Send a message to the client
char toSend[] = "Hi, I'm the server";
if (client.Send(toSend, sizeof(toSend)) != sf::Socket::Done)
// Send a message to the connected client
const char out[] = "Hi, I'm the server";
if (socket.Send(out, sizeof(out)) != sf::Socket::Done)
return;
std::cout << "Message sent to the client : \"" << toSend << "\"" << std::endl;
std::cout << "Message sent to the client: \"" << out << "\"" << std::endl;
// Receive a message back from the client
char message[128];
char in[128];
std::size_t received;
if (client.Receive(message, sizeof(message), received) != sf::Socket::Done)
if (socket.Receive(in, sizeof(in), received) != sf::Socket::Done)
return;
// Show the message
std::cout << "Message received from the client : \"" << message << "\"" << std::endl;
// Close the sockets when we're done
client.Close();
server.Close();
std::cout << "Answer received from the client: \"" << in << "\"" << std::endl;
}
////////////////////////////////////////////////////////////
/// Create a client, connect it to a server, display the
/// welcome message and send an answer.
///
////////////////////////////////////////////////////////////
void RunTcpClient(unsigned short port)
{
// Ask for the server address
sf::IpAddress server;
do
{
std::cout << "Type the address or name of the server to connect to: ";
std::cin >> server;
}
while (server == sf::IpAddress::None);
// Create a socket for communicating with the server
sf::TcpSocket socket;
// Connect to the server
if (socket.Connect(server, port) != sf::Socket::Done)
return;
std::cout << "Connected to server " << server << std::endl;
// Receive a message from the server
char in[128];
std::size_t received;
if (socket.Receive(in, sizeof(in), received) != sf::Socket::Done)
return;
std::cout << "Message received from the server: \"" << in << "\"" << std::endl;
// Send an answer to the server
const char out[] = "Hi, I'm a client";
if (socket.Send(out, sizeof(out)) != sf::Socket::Done)
return;
std::cout << "Message sent to the server: \"" << out << "\"" << std::endl;
}