Hi!
Iam rly newb in network programming, i have problem with structure of handling multiple clients with selector.
Now i do it like this:
Client class
class klient
{
public:
sf::SocketTCP sock;
sf::IPAddress adres;
string nick;
int ID;
klient(sf::SocketTCP soc, sf::IPAddress adr,string nck, int id)
{
sock = soc;
adres = adr;
nick = nck;
ID = id;
}
};
klient *klienci[10]; //array of clients
int klientnum = 0; //number of clients
Adding Clients
[...]
here is that selector thing from tutorial, Socket==Listener etc etc
[...]
sf::IPAddress Address;
sf::SocketTCP Client;
Listener.Accept(Client, &Address);
std::cout << "Client connected ! (" << Address << ")" << std::endl;
klienci[klientnum+1] = new klient(Client,Address,"default",klientnum+1); // add client data to clients array
klientnum+=1; //increse number of clients
Selector.Add(Client); //add client to selector
Recieving + (i thing wrong)Sending data
[...] its still in the selectors loop
{
sf::Packet Packet;
if (Socket.Receive(Packet) == sf::Socket::Done)
{
std::string Message;
Packet >> Message;
std::cout << "A client says : \"" << Message << "\"" << std::endl;
Socket.Send(Packet) == sf::Socket::Done;
//---------------------------here we go-----------------------------------
for(int i=1;i<klientnum;i++) //loop trough array of clients
{
if(klienci[i]->sock!=Socket) //if socket is not the same socket
{klienci[i]->sock.Send(Packet) == sf::Socket::Done;} //send data
}
}
My problem is, other clients dont get thier messages :/