0
static ArrayList<Client> clients = new ArrayList<Client>();    
while (true)
            {                                              
                Socket s = server.accept();
                System.out.println("Client connected from " + s.getLocalAddress().getHostName());  

                Thread t = new Thread(new Client(s));
                t.start();                   

            }

Simply premise, inside the Client class that just got made i am adding to a static ArrayList of 'Client' located in the main server class (above) i.e.

clients.add(Client.this);

I am then simply every 10 seconds, sending the currently online users as an object to all the clients currently in the ArrayList (Global message in effect)

 for(int i =0; i < clients.size(); i++)
                   {
                       System.out.print("sending list");
                       clients.get(i).sendList();
                   }

Now, it DOES correctly add the right number of clients etc.. and the list is gathered correctly, the client happily recieves this list every 10 seconds, UNTIL, another client connects to the server, as soon as this happens, the first client stops receiving the list and the new one takes it's place, getting all the 'received list' notifications. Whats going on here?

EDIT: sendList() code

public void sendList()
        {
            try 
            {
                ChatListObject list = new ChatListObject();
                list.setList(helper.getOnlineUsers());
                out.writeObject(list);
                out.flush();
            } 
            catch (IOException iOException) 
            {
                System.out.println(iOException);
            }
        }

Things tried for adding client:

                Client client = new Client(s);              
                Thread t = new Thread(client);
                t.start();                       
                clients.add(client);

and

clients.add(this);

in the client itself

Broak
  • 4,161
  • 4
  • 31
  • 54
  • How do you initialize "out" in Client? And how is it declared? – Samuel Åslund Mar 23 '14 at 00:19
  • in the initialize for 'Client' in = new ObjectInputStream(socket.getInputStream()); out = new ObjectOutputStream(socket.getOutputStream()); It succesfully sends messages this way bare in mind, just simply to the last connected – Broak Mar 23 '14 at 00:23
  • 1
    Please copy the actual declaration of "out", I would guess it is static and thus shared between instances of the class. That would give the symptoms you describe. – Samuel Åslund Mar 23 '14 at 00:24
  • ^ BINGO BINGO We have a winner! Thank you! Would never have spotted it myself. Post it as an answer i can accept? – Broak Mar 23 '14 at 00:28

5 Answers5

2

Please copy the actual declaration of "out", I would guess it is static and thus shared between instances of the class. That would give the symptoms you describe.

Samuel Åslund
  • 2,814
  • 2
  • 18
  • 23
1

Can you show us the method of .sendList()?

Also make sure you are doing a few things

  • Make sure Client implements Serializeable
  • Also make sure you are flushing after every write to a client
3kings
  • 838
  • 2
  • 13
  • 28
1

I recommend you put

Client myClient = new Client(s);
clients.add(myClient);
Thread t = new Thread(myClient);
datahaki
  • 600
  • 7
  • 23
  • This is how i was doing it originally, was just moving around for testing, functionally its the same. – Broak Mar 23 '14 at 00:12
1

clients.add(Client.this);

This does not do what you think it does. You want to do:

clients.add(this);

or better yet, skip the static clients list and add the client when you create the object.

Samuel Åslund
  • 2,814
  • 2
  • 18
  • 23
  • to quote myself from below "This is how i was doing it originally, was just moving around for testing, functionally its the same." I was simply adding it when making the client. changing to just 'this' also leaves the same problem – Broak Mar 23 '14 at 00:13
  • Actually there is a load of difference between the things datahaki and me suggested and what you have in the question. What you have put in the question is broken. – Samuel Åslund Mar 23 '14 at 00:22
  • What i'm saying, is, having corrected it (as in your reply) and in the method stated by datahaki the problem is still not resolved, regardless of the way i was adding the client to the list. Anyway, thank you for the response and spotting that error, however it turns out the problem was my output buffer being declared as static *doh* – Broak Mar 23 '14 at 00:30
0

Here is a sample code on Java Server with Multiclient communication in the same context that I have already posted.

It might help you to understand it.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76