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:
parent
a09ee0f9e3
commit
9280771665
66 changed files with 3976 additions and 2992 deletions
|
@ -6,14 +6,10 @@
|
|||
#include <cstdlib>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Function prototypes
|
||||
// (I'm too lazy to put them into separate headers...)
|
||||
////////////////////////////////////////////////////////////
|
||||
void DoClientTCP(unsigned short Port);
|
||||
void DoClientUDP(unsigned short Port);
|
||||
void DoServerTCP(unsigned short Port);
|
||||
void DoServerUDP(unsigned short Port);
|
||||
void RunTcpServer(unsigned short Port);
|
||||
void RunTcpClient(unsigned short Port);
|
||||
void RunUdpServer(unsigned short Port);
|
||||
void RunUdpClient(unsigned short Port);
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
@ -24,34 +20,34 @@ void DoServerUDP(unsigned short Port);
|
|||
////////////////////////////////////////////////////////////
|
||||
int main()
|
||||
{
|
||||
// Choose a random port for opening sockets (ports < 1024 are reserved)
|
||||
const unsigned short port = 2435;
|
||||
// Choose an arbitrary port for opening sockets
|
||||
const unsigned short port = 50001;
|
||||
|
||||
// TCP or UDP ?
|
||||
// TCP, UDP or connected UDP ?
|
||||
char protocol;
|
||||
std::cout << "Do you want to use TCP ('t') or UDP ('u') ? ";
|
||||
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::cout << "Do you want to be a server (s) or a client (c) ? ";
|
||||
std::cin >> who;
|
||||
|
||||
if (who == 's')
|
||||
if (protocol == 't')
|
||||
{
|
||||
// Run as a server
|
||||
if (protocol == 't')
|
||||
DoServerTCP(port);
|
||||
// Test the TCP protocol
|
||||
if (who == 's')
|
||||
RunTcpServer(port);
|
||||
else
|
||||
DoServerUDP(port);
|
||||
RunTcpClient(port);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Run as a client
|
||||
if (protocol == 't')
|
||||
DoClientTCP(port);
|
||||
// Test the unconnected UDP protocol
|
||||
if (who == 's')
|
||||
RunUdpServer(port);
|
||||
else
|
||||
DoClientUDP(port);
|
||||
RunUdpClient(port);
|
||||
}
|
||||
|
||||
// Wait until the user presses 'enter' key
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -7,59 +7,66 @@
|
|||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Create a client and send a message to a running server
|
||||
/// Launch a server, wait for a message, send an answer.
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void DoClientUDP(unsigned short port)
|
||||
void RunUdpServer(unsigned short port)
|
||||
{
|
||||
// Ask for server address
|
||||
sf::IpAddress serverAddress;
|
||||
// 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 address or name of the server to send the message to : ";
|
||||
std::cin >> serverAddress;
|
||||
std::cout << "Type the address or name of the server to connect to: ";
|
||||
std::cin >> server;
|
||||
}
|
||||
while (serverAddress == sf::IpAddress::None);
|
||||
while (server == sf::IpAddress::None);
|
||||
|
||||
// Create a UDP socket for communicating with server
|
||||
sf::SocketUDP client;
|
||||
// Create a socket for communicating with the server
|
||||
sf::UdpSocket socket;
|
||||
|
||||
// Send a message to the server
|
||||
char message[] = "Hi, I'm a client !";
|
||||
if (client.Send(message, sizeof(message), serverAddress, port) != sf::Socket::Done)
|
||||
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 server : \"" << message << "\"" << std::endl;
|
||||
std::cout << "Message sent to the server: \"" << out << "\"" << std::endl;
|
||||
|
||||
// Close the socket when we're done
|
||||
client.Close();
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Launch a server and wait for incoming messages
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void DoServerUDP(unsigned short port)
|
||||
{
|
||||
// Create a UDP socket for communicating with clients
|
||||
sf::SocketUDP server;
|
||||
|
||||
// Bind it to the specified port
|
||||
if (!server.Bind(port))
|
||||
return;
|
||||
|
||||
// Receive a message from anyone
|
||||
sf::IpAddress clientAddress;
|
||||
unsigned short clientPort;
|
||||
char message[128];
|
||||
// Receive an answer from anyone (but most likely from the server)
|
||||
char in[128];
|
||||
std::size_t received;
|
||||
if (server.Receive(message, sizeof(message), received, clientAddress, clientPort) != sf::Socket::Done)
|
||||
sf::IpAddress sender;
|
||||
unsigned short senderPort;
|
||||
if (socket.Receive(in, sizeof(in), received, sender, senderPort) != sf::Socket::Done)
|
||||
return;
|
||||
|
||||
// Display it
|
||||
std::cout << "Message received from " << clientAddress << " on port " << clientPort
|
||||
<< ": \"" << message << "\"" << std::endl;
|
||||
|
||||
// Close the socket when we're done
|
||||
server.Close();
|
||||
std::cout << "Message received from " << sender << ": \"" << in << "\"" << std::endl;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue