Applying the Java Client Server, Can not Send an ArrayList List

advertisements

I searched the internet and i didn't find something for my question. I have my client class, my server class and a Loc class. In my client class, i have a Jtext field, that i want to populate with values (free/occupied) depending if the seat is free or not.

In my server class i populate an ArrayList with all the seats free. I want to pass this ArrayList to my client and in the same time to display something like FREE FREE OCCUPIED. But i can only pass my first value. Here is the server class:

public static void main(String [] args){

ServerSocket client = null;
Loc l = new Loc();

    try {
         ArrayList<Loc> lista = new ArrayList<>();
        for (int i=0;i<7;i++)
            for(int j=0;j<6;j++)
        {
            l.setLoc(i);
            l.setRand(j);
            lista.add(l);
        }
        client = new ServerSocket(9000);
        Socket socket = client.accept();
        ObjectInputStream dis = new ObjectInputStream(socket.getInputStream());
        ObjectOutputStream dos = new ObjectOutputStream(socket.getOutputStream());

        l = (Loc) dis.readObject();
        System.out.println(l.getRand());
       // Loc a = new Loc("ags",2, 42, 125);

        dos.writeObject(lista);

    } catch (IOException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    }
}

And here is my client class:

public class Client_Gui extends javax.swing.JFrame {

Socket socket = null;
ObjectOutputStream dos = null;
ObjectInputStream dis= null;
// ObjectOutputStream dos = null;
ArrayList<Loc> lst  = new ArrayList<>();
Loc locul = new Loc();
/**
 * Creates new form Client_Gui
 */
public Client_Gui() {
    initComponents();

    Loc  loc = new Loc();
    try {
        ArrayList<Loc> lst = null;
        socket = new Socket("localhost",9000);
        dos = new ObjectOutputStream(socket.getOutputStream());
        dis = new ObjectInputStream(socket.getInputStream());
        dos.writeObject(loc);
        lst = (ArrayList<Loc>) dis.readObject();
        for(Loc a : lst){
           if(a.getIdSpectator().equals("") == true)
                        txt_area.setText("Liber");
                     else
                       txt_area.setText("Ocupat");
       }

    } catch (IOException ex) {
        Logger.getLogger(Client_Gui.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Client_Gui.class.getName()).log(Level.SEVERE, null, ex);
    }

}

And another question related to this. Can i run my client/server application without sending first some information from the client? I mean, at first i tried to read from client first and then write to server but i got errors. After i changed, and first i write from client and then read, works normally, until it reaches that part with ArrayList.

EDIT: i tried to hard code and in my Client class i put the array list created by code. Now, when i want to show it in the text field, still only the first value is displayed. Is my syntax wrong? i used:

          for(Loc a : lista){
           if(a.getIdSpectator().equals("") == true)
                        txt_area.setText("Liber");
                     else
                       txt_area.setText("Ocupat ");
            }

EDIT2: Jasper was right. I used append and now it works. Thanks.


Take a look at how you are constructing the list on the server....

ArrayList<Loc> lista = new ArrayList<>();
for (int i=0;i<7;i++)
    for(int j=0;j<6;j++)
    {
        l.setLoc(i);
        l.setRand(j);
        lista.add(l);
    }

Basically, all this is doing is adding the same instance of l to the array list, but on each loop, you are changing its properties. This means, you'll end up with a list of the same object with the same properties (cause it's just the single instance of the object)

What you should be doing is on each iteration of your loop is creating a new instance of what ever l is, setting its properties according only and that to the list

Updated

Okay, on client, you're setting the text of the JTextArea using

for(Loc a : lst){
    if(a.getIdSpectator().equals("") == true)
        txt_area.setText("Liber");
    else
        txt_area.setText("Ocupat");
}

setText replaces the contents of the field with the supplied text. Instead, you want to try using append

for(Loc a : lst){
    if(a.getIdSpectator().equals("") == true)
        txt_area.append("Liber");
    else
        txt_area.append("Ocupat");
}

Having said that, I would highly recommend that you consider taking a look at How to Use Tables and How to Use Lists for components that are better suited to display this type of data