Source code changes.
* Changed newlines to \n. * Removed whitespace before colons. * Fixed several alignments.
This commit is contained in:
parent
b27cbd5036
commit
f24ca9a840
268 changed files with 40227 additions and 40227 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue