Home arrow static arrow Java Programming [Archive] - Problem with Vectors and ObjectOutputStreams
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Problem with Vectors and ObjectOutputStreams
10 Duke Stars available
This topic has 10 replies on 1 page.

Posts:16
Registered: 7/31/04
Problem with Vectors and ObjectOutputStreams  
Aug 2, 2004 2:30 AM



 
Ok i posted some of the same stuff earlier for a different problem. I just realized that not all my clients are recieving the same information. I have implemented some output statements to show the information being sent from the server to the client and such. The server does send data to all the clients but not all the clients recieve the same data. Sound confusing? let me clarify....

----------------------Here is the main code for this on the client-------------------------------------
if(arg==login){

try{
Socket client = new Socket(ipF.getText(),4321);

ClientInfo userInfo = new ClientInfo(userNameF.getText());
frame.hide();
ChatGUI chat = new ChatGUI(userInfo.userName);
objOut = new ObjectOutputStream(client.getOutputStream());
objOut.writeObject(userInfo);
objOut.flush();

objIn = new ObjectInputStream(client.getInputStream());
}
catch(IOException ioe){

}
}
if(arg==submit){

}

}

public static void main(String args[]){
ChatGUI login = new ChatGUI();

while(true){
try{
Object c = objIn.readObject();
userList.setListData((Vector)c);
System.out.println(c);
}
catch(Exception exp){

}
}

}
}
------------------------Here is some code on the server-------------------------------------

public void run(){
try{
objIn = new ObjectInputStream(client.getInputStream());

ClientInfo x = (ClientInfo)objIn.readObject();
x.out = new PrintWriter(client.getOutputStream());
x.objOut = new ObjectOutputStream(client.getOutputStream());
usersV.add(x);
userNamesV.add(x.userName);
userList.setListData(userNamesV);

//send the vector to the clients

for(int y = 0; y < usersV.size(); y++){
x = (ClientInfo)usersV.get(y);
System.out.println(y);
System.out.println((ClientInfo)usersV.get(y));
x.objOut.writeObject(userNamesV);
x.objOut.flush();

}
}
catch(Exception ex){
ex.printStackTrace();
}
}

public static void main(String args[]) throws IOException{
ServerGUI clientT;
server = new ServerSocket(4321);
makeGUI();

while(true){

try{
clientT = new ServerGUI(server.accept());

}
catch(Exception e){
}

}

}

When a client connects ot the server the client sends an object over a stream that contains the value of the client's username and an empy variable for an object output stream. The server this deserializes this information, adds the object to a vector and adds the user's name to another vector. The vector that stores the object is mainly used to keep track of the output streams on the socket so i can output to all the clients. The other vector that stores the user name only is used to show a list of all the suers currently logged in with a JList. After the server does all this it sends back to the client the vector with all the user names so that the client can also have a list of all the people logged in.

The vectors on the server are both Static and i thought this would do the trick but no luck.

Here is an example of what happens when a client connects.

----------1st Client connects-----------------

1st Client - recieves 1st client name

----------2nd client connects------------------

1st Client- recieves 1st client name

2nd Client - recieves 1st client name, recieves 2nd client name

----------3rd client connects------------------

1st Client- recieves 1st client name

2nd Client - recieves 1st client name, recieves 2nd client name

3rd Client - recieves 1st client name, recieves 2nd client name, recieves 3rd client name

and so on. if you need the full code i uploaded it to my webserver....

www.thelastguard.com/Java/ChatGUI.java
www.thelastguard.com/Java/SeverGUI.java

 

Posts:16
Registered: 7/31/04
Re: Problem with Vectors and ObjectOutputStreams  
Aug 2, 2004 3:09 AM (reply 1 of 10)



 
Some help would be very nice right about now. Thanks
 

Posts:319
Registered: 11/8/00
Re: Problem with Vectors and ObjectOutputStreams  
Aug 2, 2004 3:20 AM (reply 2 of 10)



 
From a quick look at your code it looks that you are creating a new Server object for each connection to the server. is this what you want?
 

Posts:16
Registered: 7/31/04
Re: Problem with Vectors and ObjectOutputStreams  
Aug 2, 2004 3:27 AM (reply 3 of 10)



 
hmm yea i believe that is how it should be. When the server object is created it creates a new thread for the client that connects. I made a chat program like this before just without the object streams... i didn thave a list of the users logged in. im trying to implement that feature in this program. Well if you look at the full code maybe itll be easier to see since its formatted and such. Sorry didnt know about the code tag until after i posted, havent been on these forums much at all.
 

Posts:16
Registered: 7/31/04
Re: Problem with Vectors and ObjectOutputStreams  
Aug 2, 2004 12:01 PM (reply 4 of 10)



 
any help please?
 

Posts:6,750
Registered: 1/25/04
Re: Problem with Vectors and ObjectOutputStreams  
Aug 2, 2004 2:16 PM (reply 5 of 10)



 
From the other thread... "Im not sure if this is a problem with my vector or not but it seems that when i change a static vector in one thread of a program it doesnt change it in all the threads"

Did you synchronize access to the Vector? If not, there's no guarantee one thread will see changes made by another thread.
 

Posts:16
Registered: 7/31/04
Re: Problem with Vectors and ObjectOutputStreams  
Aug 3, 2004 12:00 AM (reply 6 of 10)



 
i tried using a synchronized method to add the obejcts to the vector but it didnt have a different effect. Please can someone look through my code really closely and run it and watch the output... this is killing me
 

Posts:37
Registered: 22/6/04
Re: Problem with Vectors and ObjectOutputStreams  
Aug 3, 2004 12:17 AM (reply 7 of 10)



 
Hi there,
I have not seen your full code..but from your sample out put i can tell u one thing that u might be using some looping mechanisam to iterate your vector.....
let me tell u first of all that try to use iterator instead of loop........second thing it may possible that when user loggs in u r not sending the updated Vector list to ur previously connected user .

see the output that you have given..........the latest vector is available to the last client only

chk it out........
 

Posts:16
Registered: 7/31/04
Re: Problem with Vectors and ObjectOutputStreams  
Aug 3, 2004 12:48 AM (reply 8 of 10)



 
".second thing it may possible that when user loggs in u r not sending the updated Vector list to ur previously connected user ."

Thats the whole problem. The previous clients do recieve the vector its just not updated....

it iterates through the vector fine i believe based on the output on the server and the fact that the clients all recieve data. I need help making it so all the clients recieve the same version of the vector when a new client connects/leaves (leaving will be implemented in later)
 

Posts:37
Registered: 22/6/04
Re: Problem with Vectors and ObjectOutputStreams  
Aug 3, 2004 1:54 AM (reply 9 of 10)



 
send me the full code at my mail id.........
 

Posts:16
Registered: 7/31/04
Re: Problem with Vectors and ObjectOutputStreams  
Aug 3, 2004 3:15 AM (reply 10 of 10)



 
Ok, i emailed you the code... as previously mentioned if anyone else who would like to help would like to see the code it is located at ......

http://www.thelastguard.com/Java/ChatGUI.java
and
http://www.thelastguard.com/Java/ServerGUI.java
 
This topic has 10 replies on 1 page.