Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Trading data between multiple clients  (Read 2688 times)

0 Members and 1 Guest are viewing this topic.

Farkraj

  • Newbie
  • *
  • Posts: 5
    • View Profile
Trading data between multiple clients
« on: December 23, 2011, 07:45:20 am »
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
Code: [Select]

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;

  }

};



Code: [Select]

klient *klienci[10];  //array of clients
int klientnum = 0;   //number of clients


Adding Clients
Code: [Select]


[...]
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
Code: [Select]

[...] 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 :/