Results 1 to 10 of 10
  1. #1
    L19htn1n9 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: ObjectInputStream not the same as ObjectOutputStream

    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?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    L19htn1n9 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default 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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: ObjectInputStream not the same as ObjectOutputStream

    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.
    Last edited by Norm; 06-27-2012 at 05:23 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    L19htn1n9 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default 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:
    Java 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:
    Java 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:
    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:
    [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

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    L19htn1n9 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default 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:
    Java 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);
    			}
    		}

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    L19htn1n9 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default 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

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. saving Object[][] via ObjectOutputStream
    By javaSweden in forum Advanced Java
    Replies: 3
    Last Post: 04-23-2012, 11:33 AM
  2. ResultSet through ObjectOutputStream
    By FlyNn in forum Advanced Java
    Replies: 4
    Last Post: 03-07-2011, 07:16 PM
  3. Replies: 17
    Last Post: 10-14-2010, 10:26 PM
  4. ObjectOutputStream Example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-20-2008, 09:21 AM
  5. question on ObjectOutputStream
    By money123 in forum New To Java
    Replies: 5
    Last Post: 07-27-2007, 10:31 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •