ObjectInputStream not the same as ObjectOutputStream
I am having a problem with my client and server.
The problem is that the object that is send to all the clients is not the same as all my clients recieve. I am sending an ArrayList with all the playerdata in it, the location, size, name color etc. But it seems that it always uses an non updated version of the arraylist.
When my client pushes the "w"/up button the client sends a message to the connection with the server, and says that the player needs to be moved up. the connection then mutates the player info and sends it along to all the clients. Only the clients do not recieve up-to-date information, the clients keep recieving the same data, that does not change: here are my files, can you guys find an error in it?
the files are zipped and must be placed in the same project.
client2.zip --> in the ClientPanel.java the sending of data takes place
client2.zip - Localhostr
server2.zip --> reading of data takes place in Connection.java
server2.zip - Localhostr
Re: ObjectInputStream not the same as ObjectOutputStream
Quote:
the clients keep recieving the same data
What set of data is that? How many sets of data does the server send? Does the client receive the first one OK?
Then on future sends of data, do the clients ever receive a new set of data or do they always receive the first set again?
Sounds like the server is not changing the data that it is sending. Can you do some debugging on the server to see if it is sending the new set of data or if it is sending the first set of data over and over again? Add printlns to print out what the server is sending each time it sends something to see if it is sending new data or old data.
If the server is sending new data every time, then is the client saving and reusing old data?
Re: ObjectInputStream not the same as ObjectOutputStream
I did some debugging, and the ArrayList that is being send by the server is up to date, but the ArrayList that the clients recieve is not
Re: ObjectInputStream not the same as ObjectOutputStream
Quote:
the ArrayList that is being send by the server is up to date
How did you determine that? Can you show the code you used to verify that. It should be right next to the statement that writes the data to the client.
Next thing to debug is to find out where and how the client is receiving the data. Does it receive more than one version of the data? Does it receive data more than one time? Is the data that is received each time the same or different?
If the client receives the same data more than one time, then where is the copy of that data being held so it is received more than one time? The only place that makes sense to me is for the server to be sending the same data more than one time.
Re: ObjectInputStream not the same as ObjectOutputStream
I added System.out.println on both the sending part and the recieving part, writing the first "player" of the ArrayList his x and y position.
I did that with this code:
Client:
Code:
@SuppressWarnings("unchecked")
private void whileChatting() throws IOException
{
//here we recieve data
do
{
try
{
Object o = input.readObject();
System.out.println(o);
if(o.getClass() == ArrayList.class)//is it a ArrayList?
{
ArrayList<PlayerData> playerList = (ArrayList<PlayerData>) o;
System.out.println("Recieving x/y 1st: " + playerList.get(0).getX() + "/" + playerList.get(0).getY()); //OVER HERE
this.panel.drawPlayers(playerList);
}
else if(o.getClass() == String.class)
{
message = (String) o;
System.out.println(message);
if(message.equals("SERVER - NAMEINUSE"))
{
JOptionPane.showMessageDialog(null, "Je bent gekickt van de server\nJe naam is al ingebruik!", "Melding!", JOptionPane.INFORMATION_MESSAGE);
this.closeRunning();
}
else if(message.equals("SERVER - END"))
this.closeRunning();
else
System.out.println(message);
}
}
catch(ClassNotFoundException ex)
{
System.out.println("class not found: " + ex.getMessage());
}
}
while(!message.equals("SERVER - END"));
and the Connection:
Code:
public void sendGeneralData()
{
ArrayList<PlayerData> pl = new ArrayList<PlayerData>();
pl.clear();
ArrayList<Connection> cl = this.server.getConnectionList();
for(Connection c: cl)
{
pl.add(c.getPd());
}
System.out.println("Sending x/y 1st: " + pl.get(0).getX() + "/" + pl.get(0).getY());
this.sendMessage(pl);
}
the server says this in the console:
Quote:
Sending x/y 1st: 10/10
Org x/y: 10/10 | new x/y: 10/0
Sending x/y 1st: 10/0
Org x/y: 10/0 | new x/y: 10/-10
Sending x/y 1st: 10/-10
The client says this:
Quote:
[server2.PlayerData@623c88ed]
Recieving x/y 1st: 10/10
[server2.PlayerData@623c88ed]
Recieving x/y 1st: 10/10
[server2.PlayerData@623c88ed]
Recieving x/y 1st: 10/10
Re: ObjectInputStream not the same as ObjectOutputStream
What code is in sendMessage() ?
What is the definition of the variable: input? How is it given a value?
Re: ObjectInputStream not the same as ObjectOutputStream
I got 2 sendMessage() functions, one for sending strings and one for sending an ArrayList, but here it is:
Code:
//send a message to client
private void sendMessage(String message)
{
try
{
output.writeObject("SERVER - " + message);
output.flush();
}
catch(IOException ex)
{
//cant send the message
}
}
private void sendMessage(ArrayList<PlayerData> pl)
{
try
{
output.writeObject(pl);
output.flush();
}
catch(IOException ex)
{
//cant send the message
System.out.println("cant send: " + ex);
}
}
Re: ObjectInputStream not the same as ObjectOutputStream
Can you make a small simple program with both client and server that compiles, executes and shows the problem?
For debugging you need a println in the sendMessage() method that prints out the object that is being written to output like in the whileChatting() method.
Re: ObjectInputStream not the same as ObjectOutputStream
I have given the entire source code in the first message, you could import it into eclipse or something and run the project locally
Re: ObjectInputStream not the same as ObjectOutputStream
I don't use any IDE. I have an enhanced editor. More than 2 files are too much to handle in my environment.