Added support for the CMake build system

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1550 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
LaurentGom 2010-08-19 15:59:24 +00:00
parent aa612ec63a
commit a991fe8e4d
144 changed files with 2086 additions and 7033 deletions

View file

@ -0,0 +1,12 @@
set(SRCROOT ${CMAKE_SOURCE_DIR}/examples/sockets)
# all source files
set(SRC ${SRCROOT}/Sockets.cpp
${SRCROOT}/TCP.cpp
${SRCROOT}/UDP.cpp)
# define the sockets target
sfml_add_example(sockets
SOURCES ${SRC}
DEPENDS sfml-network sfml-system)

View file

@ -0,0 +1,59 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <iostream>
#include <cstdlib>
void RunTcpServer(unsigned short Port);
void RunTcpClient(unsigned short Port);
void RunUdpServer(unsigned short Port);
void RunUdpClient(unsigned short Port);
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
// Choose an arbitrary port for opening sockets
const unsigned short port = 50001;
// TCP, UDP or connected UDP ?
char protocol;
std::cout << "Do you want to use TCP (t) or UDP (u) ? ";
std::cin >> protocol;
// Client or server ?
char who;
std::cout << "Do you want to be a server (s) or a client (c) ? ";
std::cin >> who;
if (protocol == 't')
{
// Test the TCP protocol
if (who == 's')
RunTcpServer(port);
else
RunTcpClient(port);
}
else
{
// Test the unconnected UDP protocol
if (who == 's')
RunUdpServer(port);
else
RunUdpClient(port);
}
// Wait until the user presses 'enter' key
std::cout << "Press enter to exit..." << std::endl;
std::cin.ignore(10000, '\n');
std::cin.ignore(10000, '\n');
return EXIT_SUCCESS;
}

81
examples/sockets/TCP.cpp Normal file
View file

@ -0,0 +1,81 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Network.hpp>
#include <iostream>
////////////////////////////////////////////////////////////
/// Launch a server, wait for an incoming connection,
/// send a message and wait for the answer.
///
////////////////////////////////////////////////////////////
void RunTcpServer(unsigned short port)
{
// Create a server socket to accept new connections
sf::TcpListener listener;
// 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::TcpSocket socket;
if (listener.Accept(socket) != sf::Socket::Done)
return;
std::cout << "Client connected: " << socket.GetRemoteAddress() << std::endl;
// 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: \"" << out << "\"" << std::endl;
// Receive a message back from the client
char in[128];
std::size_t received;
if (socket.Receive(in, sizeof(in), received) != sf::Socket::Done)
return;
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;
}

72
examples/sockets/UDP.cpp Normal file
View file

@ -0,0 +1,72 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Network.hpp>
#include <iostream>
////////////////////////////////////////////////////////////
/// Launch a server, wait for a message, send an answer.
///
////////////////////////////////////////////////////////////
void RunUdpServer(unsigned short port)
{
// Create a socket to receive a message from anyone
sf::UdpSocket socket;
// Listen to messages on the specified port
if (socket.Bind(port) != sf::Socket::Done)
return;
std::cout << "Server is listening to port " << port << ", waiting for a message... " << std::endl;
// Wait for a message
char in[128];
std::size_t received;
sf::IpAddress sender;
unsigned short senderPort;
if (socket.Receive(in, sizeof(in), received, sender, senderPort) != sf::Socket::Done)
return;
std::cout << "Message received from client " << sender << ": \"" << in << "\"" << std::endl;
// Send an answer to the client
const char out[] = "Hi, I'm the server";
if (socket.Send(out, sizeof(out), sender, senderPort) != sf::Socket::Done)
return;
std::cout << "Message sent to the client: \"" << out << "\"" << std::endl;
}
////////////////////////////////////////////////////////////
/// Send a message to the server, wait for the answer
///
////////////////////////////////////////////////////////////
void RunUdpClient(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::UdpSocket socket;
// Send a message to the server
const char out[] = "Hi, I'm a client";
if (socket.Send(out, sizeof(out), server, port) != sf::Socket::Done)
return;
std::cout << "Message sent to the server: \"" << out << "\"" << std::endl;
// Receive an answer from anyone (but most likely from the server)
char in[128];
std::size_t received;
sf::IpAddress sender;
unsigned short senderPort;
if (socket.Receive(in, sizeof(in), received, sender, senderPort) != sf::Socket::Done)
return;
std::cout << "Message received from " << sender << ": \"" << in << "\"" << std::endl;
}