Arraylist Improperly transfered through ObjectStreams

code from client:

publicvoid listen()

{

String readtext;

field.setText("");

System.out.println("Starting to Listen");

while(!socket.isClosed()){

try{

dataType obj = (dataType)(in.readObject());

switch(obj)

{

case String:

readtext = in.readUTF();

field.append(readtext);

System.out.println("Text recieved: " + readtext);

break;

case Userlist:

System.out.println("USERLIST UPDATE");

setUserList((Userlist)(in.readObject()));

break;

}

}catch (IOException e){}

catch(ClassNotFoundException cnfe){cnfe.printStackTrace();}

}

}

privatevoid setUserList(Userlist ul)

{

System.out.println((ul.size()) +" Users");

userlist.removeAll();

for(int i = 0; i < ul.size(); i++)

userlist.add(((User)(ul.get(i))).getName());

}

code from server's client threads:ClientWorker(Socket client, Server s){

this.client = client;

this.server = s;

}

publicvoid run(){

String line;

try{

in =new ObjectInputStream(client.getInputStream());

out =new ObjectOutputStream(client.getOutputStream());

}catch (IOException e){

System.out.println("in or out failed");

System.exit(-1);

}

try{

String userName = in.readUTF();

This =new User(userName);

server.UserNames.add(This);

server.updateList();

userset =true;

}catch(IOException ieo){}

...

code from server:publicvoid updateList()

{

System.out.println((UserNames.size()) +" Users");

for(int i = 0; i < Users.size(); i++)

try{((ClientWorker)Users.get(i)).getOutputStream().writeObject(dataType.Userlist);

((ClientWorker)Users.get(i)).getOutputStream().writeObject(UserNames);

}

catch(IOException e){e.printStackTrace();}

}

this code improperly prints the userlist (extends Arraylist) usernames object from the server to the clients

for one client, userlist usernames.size() = 1, works for both server and client...

for two clients:

client one:

userlist usernames.size() = 1 <- incorrect

client two:

userlist usernames.size() = 2 <- correct

it seems that after the first time the server sends the client the list that the list doesnt change on the client even though it is sending the same userlist object to all the clients

[4518 byte] By [sosleepya] at [2007-10-3 10:45:37]
# 1
Your posted code is too fragmented to get clear understanding but if you are sending a same object repeatedly, try using writeUnshared() method instead of writeObject().
hiwaa at 2007-7-15 6:09:58 > top of Java-index,Archived Forums,Socket Programming...
# 2
ty again :Di really should look to the documentation of where the problem occurs next time i have a question
sosleepya at 2007-7-15 6:09:58 > top of Java-index,Archived Forums,Socket Programming...