Hey,
I'm using a selector and all works fine but I don't know how to identificate clients. For example I want to give them numbers. Is there a nice way to do this with help of SFML?
const unsigned int numSockets = selector.Wait();
for (unsigned int i = 0; i < numSockets; ++i)
{
sf::SocketTCP socket = selector.GetSocketReady(i);
if (socket == listener)
{
sf::IPAddress address;
sf::SocketTCP client;
listener.Accept(client, &address);
selector.Add(client);
}
else
{
char recvBuf[1024];
size_t received;
if (socket.Receive(recvBuf, sizeof(recvBuf) - 1, received) != sf::Socket::Done)
// ----------> - 1 is needed, I think. But I didn't see it in tutorial.
{
selector.Remove(socket);
std::cerr << "Error: receive" << std::endl;
continue;
}
recvBuf[received] = '\0'; // ----------> This is needed, I think. But I didn't see it in tutorial.
std::cout << "Client (" << i << ") : " << recvBuf << std::endl; // ----------> I hoped I could give numbers to them this way. But all clients got the number 0.
std::string sndBuf;
sndBuf = "Thanks for the message: ";
sndBuf.append(recvBuf);
if (socket.Send(sndBuf.c_str(), sndBuf.length()) != sf::Socket::Done)
{
selector.Remove(socket);
std::cerr << "Error: Send" << std::endl;
continue;
}
}
}
Any suggestions?
Thanks in advance,
Dummie