The program should run as follows: Client sends a message to the server, server then sends that message to all clients. Right now, the server wont send any message back except the first one the client sends.
In case im not clear, here is an illustration of the output:
1 2 3 4
client Sent Message:Test A
client Received Message:Test A
client Sent Message:New Message
client Received Message:Test A
Here is the relevant code in the client and server cpp files
std::vector<sf::SocketTCP> SocketsReady;
while( true )
{
unsignedint NbSockets = Selector.Wait();
for (unsignedint i = 0; i < NbSockets; ++i)
{
//Get the current socket
sf::SocketTCP Socket = Selector.GetSocketReady(i);
if( Socket == socket )
{
// If the listening socket is ready, it means
// that we can accept a new connection
sf::IPAddress Address;
sf::SocketTCP Client;
socket.Accept(Client, &Address);
std::cout << "Client connected! (" << Address << ")"<<std::endl;
//Add it to the selector
Selector.Add(Client);
}
else
{
// Else, it is a client socket se we can read
// the data sent from it
sf::Packet ReceivePacket;
if( Socket.Receive(ReceivePacket) == sf::Socket::Done)
{
// Extract the message and display it
std::string ReceiveMessage;
ReceivePacket >> ReceiveMessage;
std::cout << "Received Message : " <<ReceiveMessage << std::endl;
//Send the message to all clients
for( unsignedint j = 0; j < NbSockets; ++i)
{
sf::Packet SendPacket;
std::cout<<"Sending message : "<<ReceiveMessage<<std::endl;
SendPacket << ReceiveMessage;
if(Socket.Send(SendPacket) != sf::Socket::Done)
std::cout<<"Error sending a message to all clients"<<std::endl;
}
}
else
{
std::cout << "Error receiving message"<< std::endl;
// Error : we'd better remove the scoket from the selector
Selector.Remove(Socket);
}
}
}
}