This may enter the realm of stupidity here but I wanted to make sure I'm understanding the tutorial for SFML 2.1's Networking module. The example code and accompanying text skims over how two or more triggering connections are handled using sf::Sockets. My understanding was that, if I had a TCP server-client relation program, the server would need one listener socket for incoming connections and one socket for every client that connects. This seems to be exactly what this line in the tutorial states:
On server side, more things need to be done. Multiple sockets are required: one that listens for incoming connections, and one for each connected client.
The code in the example however only provides the example for the situation of having only one server and one client connecting at a time as far as I can tell:
sf::TcpListener listener;
// bind the listener to a port
if (listener.listen(53000) != sf::Socket::Done)
{
// error...
}
// accept a new connection
sf::TcpSocket client;
if (listener.accept(client) != sf::Socket::Done)
{
// error...
}
// use "client" to communicate with the connected client,
// and continue to accept new connections with the listener
As I have understood it ( and the comments at the bottom of the tutorial code states this ), all I need is for the clients to know where to find the listener socket ( IP address and port it's listening from ) then the listener will accept the connection and return a successfully connected client in a sf::Socket of my choice to handle all further interactions with this unique client until they disconnect thus freeing the socket for a new client.
Going with my logic flow earlier I figured all I need to do is if I want say five clients to be able to be connected to the server all at the same time then I need six sockets; one to listen for new connections and one to handle the transfer of information to and from each client ( so five of these as this example wants five clients ). So my code might at first look like this:
sf::TcpListener listener;
// bind the listener to a port
if (listener.listen(53000) != sf::Socket::Done)
{
// error...
}
while(running)
{
// accept a new connection
sf::TcpSocket client1;
sf::TcpSocket client2;
sf::TcpSocket client3;
sf::TcpSocket client4;
sf::TcpSocket client5;
// Check to see if client1 is being used and if not give the new client this socket.
if(client1 == openForNewClient)
{
if (listener.accept(client1) != sf::Socket::Done)
{
// error...
}
}else
// Check to see if client2 is being used and if not give the new client this socket.
if(client2 == openForNewClient)
{
if (listener.accept(client2) != sf::Socket::Done)
{
// error...
}
}else
// Check to see if client3 is being used and if not give the new client this socket.
if(client3 == openForNewClient)
{
if (listener.accept(client3) != sf::Socket::Done)
{
// error...
}
}else
// Check to see if client4 is being used and if not give the new client this socket.
if(client4 == openForNewClient)
{
if (listener.accept(client4) != sf::Socket::Done)
{
// error...
}
}else
// Check to see if client5 is being used and if not give the new client this socket.
if(client5 == openForNewClient)
{
if (listener.accept(client5) != sf::Socket::Done)
{
// error...
}
}else
{
// Inform potential client that there is no slot ( socket ) available.
// The client doesn't connect.
}
// See if client1 is sending data or needs data sent.
// See if client2 is sending data or needs data sent.
// See if client3 is sending data or needs data sent.
// See if client4 is sending data or needs data sent.
// See if client5 is sending data or needs data sent.
// Update server's local frame of information based on what was received by clients.
// See if client1 has disconnected and therefore has freed the socket they were using.
// See if client2 has disconnected and therefore has freed the socket they were using.
// See if client3 has disconnected and therefore has freed the socket they were using.
// See if client4 has disconnected and therefore has freed the socket they were using.
// See if client5 has disconnected and therefore has freed the socket they were using.
}
Basically I want to know how to determine if a socket is in use by a client so that I can let my listening socket assign them with the correct socket. If there's no socket available then they simply don't connect. However sf::Socket::Status doesn't have a "In-use" enum, but I figured that's what "NotReady" was for. Am I right and would checking the status for "NotReady" being true determine whether a socket is in use by another client or not? Am I missing something obvious here? Is the "Disconnected" enum not just for the action of closing one of the two connected sockets from either the client or server?