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

    Default ObjectInputStream not reading my object via networking

    My problem is that some how the object that i send from the server to the client does not get to the client side, or the client does not read it, String messages get read withouth a problem, but when i send a ArrayList it just does not read it. Here is my server code:
    Java Code:
    package server2;
    
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.EOFException;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.*;
    import java.util.ArrayList;
    
    import javax.swing.JTextArea;
    import javax.swing.Timer;
    
    public class Connection implements Runnable ,ActionListener
    {
    
    	private Socket connection;
    	private ObjectOutputStream output;
    	private ObjectInputStream input;
    	private Server server;
    	private String name;
    	private PlayerData pd;
    	private Timer autoSend;
    	
    	public Connection(Socket connection, JTextArea chatWindow, Server server)
    	{
    		this.connection = connection;
    		this.server = server;
    		this.autoSend = new Timer(50, this);
    	}
    	
    	public void run()
    	{
    		try
    		{
    			setupStreams();
    			whileChatting();
    		}
    		catch(EOFException eofEx)
    		{}
    		catch (IOException e)
    		{
    			e.printStackTrace();
    			closeRunning();
    		}
    		finally
    		{
    			closeRunning();
    		}
    	}
    	
    	//get stream to send and recieve data
    		private void setupStreams() throws IOException
    		{
    			output = new ObjectOutputStream(connection.getOutputStream());
    			output.flush();
    			output.reset();
    			
    			input = new ObjectInputStream(connection.getInputStream());
    		}
    		
    		//during the chat conversation
    		public void whileChatting() throws IOException
    		{
    			try
    			{
    				name = (String) input.readObject();
    				this.server.addConnection(this.name, this);
    				this.autoSend.start();
    				
    				//here we make a playerdata object
    				pd = new PlayerData(10, 10, 20, Color.green, name);
    			} catch (ClassNotFoundException e)
    			{
    				e.printStackTrace();
    			}
    			
    			String message = " You are now connected! ";
    			do
    			{
    				//have a conversation
    				try
    				{
    					message = (String) input.readObject();
    					if(message.substring(0, 1).equals("m"))//we want to move the character
    					{
    						String values = message.substring(0);
    						int index = values.indexOf(',');
    						String valueX = values.substring(0, index);
    						String valueY = values.substring(index + 1);
    						System.out.println("X = " + valueX + "| Y = " + valueY);
    					}
    				}
    				catch(ClassNotFoundException CNFEx)
    				{}
    			}
    			while(!message.equals("CLIENT - END"));
    		}
    		
    		//close streams and sockets after you are done chatting
    		private void closeRunning()
    		{
    			try
    			{
    				this.server.removeConnection(this.name, this);
    				this.autoSend.stop();
    				output.close();
    				input.close();
    				connection.close();
    			}
    			catch(IOException ex)
    			{
    				ex.printStackTrace();
    			}
    		}
    		
    		//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();
    				System.out.println("Sending info");
    			}
    			catch(IOException ex)
    			{
    				//cant send the message
    				System.out.println("cant send: " + ex);
    			}
    		}
    
    		public PlayerData getPd()
    		{
    			return pd;
    		}
    
    		public void setPd(PlayerData pd)
    		{
    			this.pd = pd;
    		}
    
    		@Override
    		public void actionPerformed(ActionEvent arg0)
    		{
    			this.sendGeneralData();
    		}
    		
    		public void sendGeneralData()
    		{
    			ArrayList<PlayerData> pl = new ArrayList<PlayerData>();
    			ArrayList<Connection> cl = this.server.getConnectionList();
    			for(Connection c: cl)
    			{
    				pl.add(c.getPd());
    			}
    			this.sendMessage(pl);
    		}
    
    }
    and my client:
    Java Code:
    package client2;
    
    import java.io.*;
    import java.net.*;
    import java.util.ArrayList;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class Client extends JFrame
    {
    	private ObjectOutputStream output;
    	private ObjectInputStream input;
    	private String message = "";
    	private String serverIP;
    	private Socket connection;
    	private String name= "";
    	private ClientPanel panel;
    	
    	//constructor
    	public Client(String host)
    	{
    		this.setTitle("Client F-IM");
    		this.name = JOptionPane.showInputDialog(null, "Wat is je naam?");
    		this.serverIP = host;
    		this.setSize(800, 600);
    		
    		this.panel = new ClientPanel();
    		this.add(this.panel);
    		
    		this.setLocationRelativeTo(null);
    		this.setVisible(true);
    	}
    	
    	//connect to server
    	public void startRunning()
    	{
    		try
    		{
    			connectToServer();
    			setupStreams();
    			whileChatting();
    		}
    		catch(EOFException EOFEx)
    		{}
    		catch(IOException ex)
    		{
    			ex.printStackTrace();
    		}
    		finally
    		{
    			closeRunning();
    		}
    	}
    	
    	//connect to server
    	private void connectToServer() throws IOException
    	{
    		connection = new Socket(InetAddress.getByName(serverIP), 6789);
    	}
    	
    	//set up streams to send and recieve messages
    	private void setupStreams() throws IOException
    	{
    		output = new ObjectOutputStream(connection.getOutputStream());
    		output.flush();
    		input = new ObjectInputStream(connection.getInputStream());
    		//send the basic data
    		this.sendName(name);
    	}
    	
    	private void sendName(String name2)
    	{
    		try
    		{
    			output.writeObject(name2);
    			output.flush();
    		}
    		catch(IOException ex)
    		{
    			
    		}
    	}
    
    	//while chatting with server
    	@SuppressWarnings("unchecked")
    	private void whileChatting() throws IOException
    	{
    		//hier ontvangen we alle data
    		this.sendData("m10,500");
    		do
    		{
    			try
    			{
    				//message = (String) input.readObject();
    				//System.out.println(message);
    				
    				ArrayList<PlayerData>  playerList = (ArrayList<PlayerData>) input.readObject();
    				System.out.println("recieved player data");
    				this.panel.drawPlayers(playerList);	
    			}
    			catch(ClassNotFoundException ex)
    			{
    				//class not found
    			}
    		}
    		while(!message.equals(name + " - END"));
    	}
    	
    	//close the streams and sockets
    	private void closeRunning()
    	{		
    		try
    		{
    			output.close();
    			input.close();
    			connection.close();
    		}
    		catch(IOException ex)
    		{
    			ex.printStackTrace();
    		}
    	}
    	
    	//send messages to server
    	private void sendData(String message)
    	{
    		try
    		{
    			output.writeObject(message);
    			output.flush();
    		}
    		catch(IOException ex)
    		{
    			
    		}
    	}
    }
    I hope you guys can help me with this.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: ObjectInputStream not reading my object via networking

    it just does not read it.
    Can you explain what happens and post any error messages?

    The posted code does not compile because of missing classes. Can you make a small program that compiles, executes and shows the problem?
    Last edited by Norm; 06-27-2012 at 03:41 AM.
    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 reading my object via networking


  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: ObjectInputStream not reading my object via networking

    Java Code:
    catch(ClassNotFoundException ex)
    {                 //class not found             }
    and
    Java Code:
    catch(IOException ex)
    {                       }
    You're eating exceptions all over the place.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: ObjectInputStream not reading my object via networking

    that is not the problem right now, the program is not finished, but the point is that the cleint does not recieve the object

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

    Default Re: ObjectInputStream not reading my object via networking

    I added a System.out.println to the client class not found exception when recieving data, and it seems that the exception is thrown, this is the error
    class not found: server2.PlayerData
    so somehow i need to find a way to import the PlayerData class from my server application

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: ObjectInputStream not reading my object via networking

    Thank you.
    So it was the problem.
    Never eat exceptions!

    This has been a Public Service Announcement.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Trouble with reading csv into object array
    By shaunstruwig in forum New To Java
    Replies: 1
    Last Post: 06-07-2012, 04:36 PM
  2. writing and Reading a serialized java object
    By KunCode in forum New To Java
    Replies: 2
    Last Post: 05-20-2012, 07:51 PM
  3. Reading object in multithreaded socket server
    By dario111cro in forum Networking
    Replies: 1
    Last Post: 02-07-2012, 12:04 AM
  4. Replies: 3
    Last Post: 11-17-2010, 06:47 PM
  5. Reading from ObjectInputStream
    By deepthought015 in forum Networking
    Replies: 8
    Last Post: 04-28-2009, 06:57 PM

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
  •