Source code changes.

* Changed newlines to \n.
* Removed whitespace before colons.
* Fixed several alignments.
This commit is contained in:
Stefan Schindler 2014-09-30 16:07:25 +02:00
parent b27cbd5036
commit f24ca9a840
268 changed files with 40227 additions and 40227 deletions

View file

@ -1,12 +1,12 @@
set(SRCROOT ${PROJECT_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)
set(SRCROOT ${PROJECT_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

@ -1,59 +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;
}
////////////////////////////////////////////////////////////
// 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;
}

View file

@ -1,81 +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;
}
////////////////////////////////////////////////////////////
// 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;
}

View file

@ -1,72 +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;
}
////////////////////////////////////////////////////////////
// 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;
}