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

@ -90,7 +90,7 @@ int main()
case 1 :
{
// Print current server directory
// Print the current server directory
sf::Ftp::DirectoryResponse response = server.GetWorkingDirectory();
std::cout << response << std::endl;
std::cout << "Current directory is " << response.GetDirectory() << std::endl;
@ -99,11 +99,12 @@ int main()
case 2 :
{
// Print content of current server directory
// Print the contents of the current server directory
sf::Ftp::ListingResponse response = server.GetDirectoryListing();
std::cout << response << std::endl;
for (std::size_t i = 0; i < response.GetCount(); ++i)
std::cout << response.GetFilename(i) << std::endl;
std::vector<std::string> filenames = response.GetFilenames();
for (std::vector<std::string>::const_iterator it = filenames.begin(); it != filenames.end(); ++it)
std::cout << *it << std::endl;
break;
}
@ -123,7 +124,7 @@ int main()
std::string directory;
std::cout << "Name of the directory to create: ";
std::cin >> directory;
std::cout << server.MakeDirectory(directory) << std::endl;
std::cout << server.CreateDirectory(directory) << std::endl;
break;
}

View file

@ -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

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;
}

View file

@ -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;
}

View file

@ -22,17 +22,35 @@ public :
////////////////////////////////////////////////////////////
/// Constructor
///
/// \param Socket : Socket that holds the connection with the server
/// \param host Remote host to which send the recording data
/// \param port Port of the remote host
///
////////////////////////////////////////////////////////////
NetworkRecorder(sf::SocketTCP socket) :
mySocket(socket)
NetworkRecorder(const sf::IpAddress& host, unsigned short port) :
myHost(host),
myPort(port)
{
}
private :
////////////////////////////////////////////////////////////
/// /see SoundRecorder::OnStart
///
////////////////////////////////////////////////////////////
virtual bool OnStart()
{
if (mySocket.Connect(myHost, myPort) == sf::Socket::Done)
{
std::cout << "Connected to server " << myHost << std::endl;
return true;
}
else
{
return false;
}
}
////////////////////////////////////////////////////////////
/// /see SoundRecorder::ProcessSamples
///
@ -48,10 +66,27 @@ private :
return mySocket.Send(packet) == sf::Socket::Done;
}
////////////////////////////////////////////////////////////
/// /see SoundRecorder::OnStop
///
////////////////////////////////////////////////////////////
virtual void OnStop()
{
// Send a "end-of-stream" packet
sf::Packet packet;
packet << endOfStream;
mySocket.Send(packet);
// Close the socket
mySocket.Disconnect();
}
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
sf::SocketTCP mySocket; ///< Socket used to communicate with the server
sf::IpAddress myHost; ///< Address of the remote host
unsigned short myPort; ///< Remote port
sf::TcpSocket mySocket; ///< Socket used to communicate with the server
};
@ -70,41 +105,25 @@ void DoClient(unsigned short port)
}
// Ask for server address
sf::IpAddress serverAddress;
sf::IpAddress server;
do
{
std::cout << "Type address or name of the server to connect to : ";
std::cin >> serverAddress;
std::cin >> server;
}
while (serverAddress == sf::IpAddress::None);
while (server == sf::IpAddress::None);
// Create a TCP socket for communicating with server
sf::SocketTCP socket;
// Connect to the specified server
if (socket.Connect(port, serverAddress) != sf::Socket::Done)
return;
std::cout << "Connected to server " << serverAddress << std::endl;
// Create an instance of our custom recorder
NetworkRecorder recorder(server, port);
// Wait for user input...
std::cin.ignore(10000, '\n');
std::cout << "Press enter to start recording audio";
std::cin.ignore(10000, '\n');
// Create a instance of our custom recorder
NetworkRecorder recorder(socket);
// Start capturing audio data
recorder.Start(44100);
std::cout << "Recording... press enter to stop";
std::cin.ignore(10000, '\n');
recorder.Stop();
// Send a "end-of-stream" packet
sf::Packet packet;
packet << endOfStream;
socket.Send(packet);
// Close the socket when we're done
socket.Close();
}

View file

@ -32,17 +32,6 @@ public :
Initialize(1, 44100);
}
////////////////////////////////////////////////////////////
/// Destructor
///
////////////////////////////////////////////////////////////
~NetworkAudioStream()
{
// Close the sockets
myClient.Close();
myListener.Close();
}
////////////////////////////////////////////////////////////
/// Run the server, stream audio data from the client
///
@ -52,14 +41,14 @@ public :
if (!myHasFinished)
{
// Listen to the given port for incoming connections
if (!myListener.Listen(port))
if (myListener.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;
myListener.Accept(myClient, &clientAddress);
std::cout << "Client connected : " << clientAddress << std::endl;
if (myListener.Accept(myClient) != sf::Socket::Done)
return;
std::cout << "Client connected: " << myClient.GetRemoteAddress() << std::endl;
// Start playback
Play();
@ -149,7 +138,7 @@ private :
else if (id == endOfStream)
{
// End of stream reached : we stop receiving audio data
std::cout << "Audio data has been 100% received !" << std::endl;
std::cout << "Audio data has been 100% received!" << std::endl;
myHasFinished = true;
}
else
@ -164,8 +153,8 @@ private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
sf::SocketTCP myListener;
sf::SocketTCP myClient;
sf::TcpListener myListener;
sf::TcpSocket myClient;
sf::Mutex myMutex;
std::vector<sf::Int16> mySamples;
std::vector<sf::Int16> myTempBuffer;