Results 1 to 3 of 3
  1. #1
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default using ObjectOutputStream and serialization

    Hi, I am trying to send a simple serializable object to a server but I am not sure if I really can send it this way like im doing here:


    Java Code:
    /**
     * client send/receive thread
     */
    public class OoClientThread implements Runnable
    {
    	private int				mPort;			// server port
    	private String			mHost;			// server host
    	private Socket			mClientSocket;	        // socket connection
    	
    	private GUIclientRoom		mGUI;			// GUI class
    	
    	private DataOutputStream	mDos;			// output stream
    	private DataInputStream	mDis;			        // input stream
    	
    	/**
    	 * Constructor
    	 */
    	public OoClientThread(String inHost, int inPort, GUIclientRoom inGUI) throws Exception
    	{
    		mGUI = inGUI;
    		mHost = inHost;
    		mPort = inPort;
    		
    		mClientSocket = new Socket(mHost, mPort); // connect to server
    	}
    	
    	@Override
    	public void run()
    	{
    		try
    		{
    			// STREAM
    			mDos = new DataOutputStream(new BufferedOutputStream(mClientSocket.getOutputStream()));
    			mDis = new DataInputStream(new BufferedInputStream(mClientSocket.getInputStream()));
    			
    			
    			// USER
    			writeObject(mGUI.getUserObject()); // send user object at join
    			
    			// THREAD work
    			while(true)
    			{
    				
    				// soem code ...
    				
    				Thread.sleep(75);
    			}
    		}
    		catch(IOException | InterruptedException ex)
    		{
    			System.out.println("client error: " + ex);
    		}
    	}
    	
    	/**
    	 * write outgoing objects to the stream
    	 * 
    	 * @param inObj
    	 *            Object to send
    	 * @throws IOException
    	 */
    	private void writeObject(OoClientUser inObj) throws IOException
    	{
    		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    		ObjectOutputStream oos = new ObjectOutputStream(baos);
    		
    		mDos.writeUTF("USER");
    		byte[] bytes = baos.toByteArray();
    		mDos.writeInt(bytes.length);
    		mDos.write(bytes);
    		mDos.flush();
    		
    		System.out.println("sending " + inObj);
    		
    		oos.writeObject(inObj);
    		oos.close();
    	}
    }
    This is the user object that I want to send:

    Java Code:
    public class OoClientUser implements java.io.Serializable
    {
    	/**
    	 * version
    	 */
    	private static final long	serialVersionUID	= 1_5L;
    	private String	mUserName;	// username
    															
    	/**
    	 * constructor
    	 */
    	public OoClientUser(String inName)
    	{
    		mUserName = inName;
    	}
    	
    	/**
    	 * get the username
    	 * 
    	 * @return Name of user
    	 */
    	public String getName()
    	{
    		return mUserName;
    	}
    }
    On my server I just recieve null when using that and this is how I handle the deserialization on the server:

    Java Code:
    private void readObject() throws IOException
    	{
    		OoServerUser s = null;
    		
    		int length = mDis.readInt(); // read length
    		byte[] data = new byte[length]; // create byte array
    		mDis.readFully(data); // fill byte array
    		
    		ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
    		
    		try
    		{
    			s =  (OoServerUser) ois.readObject(); // create object
    			
    		    ois.close();
    		}
    		catch(EOFException e)
    		{
    			mGUI.addUser(s);
    			System.out.println("added user " + s);
    		}
    		catch(ClassNotFoundException e)
    		{
    			System.out.println(e);
    		}
    	}
    The class OoServerUser is not identical to OoClientUser which im sending because OoServerUser contains one more method, is this a problem?

    Thanks for reading!
    Last edited by Gatsu; 08-10-2013 at 04:37 PM.

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

    Default Re: using ObjectOutputStream and serialization

    The objec being written and read should be the same version of the same class.

    NOTE: For testing class and method usage, it would be simpler to write to a file and read from that file instead of using Sockets.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: using ObjectOutputStream and serialization

    I believe the true problem was that I didn't really understand the method writeObject, I solved this by rearranging that method like this:

    Java Code:
    private void writeObject(OoUser inObj) throws IOException, ClassNotFoundException
    	{
    		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    		ObjectOutputStream oos = new ObjectOutputStream(baos);
    		
    		oos.writeObject(inObj);
    		
    		mDos.writeUTF("USER");
    		byte[] bytes = baos.toByteArray();
    		mDos.writeInt(bytes.length);
    		mDos.write(bytes);
    		mDos.flush();
    		
    		oos.close();
    	}
    Thank you !

Similar Threads

  1. Replies: 3
    Last Post: 02-08-2011, 07:45 PM
  2. Replies: 17
    Last Post: 10-14-2010, 10:26 PM
  3. Replies: 0
    Last Post: 04-04-2008, 02:47 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
  •