Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Socket/ObjectOutputStream/Very Simple Object serialization error

    Hi,

    I'm having difficulty with my 'implements serializable' class that I am trying to serialize and then pass over the network. (currently sending and receiving messages using LocalHost loopback address)
    My class is called 'SerializedClass.java', and it's pretty simple, so I'm unsure what formatting mistakes I'm making. Everything I look at online implies to me that this is correct... Hm.

    Java Code:
    import java.io.Serializable;
    public class SerializedClass implements Serializable {
        String string;
        SerializedClass()
        {
            string = "String.";
        }
    }
    The code snippets that send it from the client are as follows (I left out the 'tries' and 'catches', here... but they are there and functioning fine.)
    Client code:
    Java Code:
    //Serialization test
    SerializedClass seriously;
    Address = InetAddress.getLocalHost();
    socket = new Socket(Address, port);
    objectOut = new ObjectOutputStream(socket.getOutputStream());
    objectOut.flush();
    seriously = new SerializedClass();
    Finally, I am sending this message up to 60 times per second if any keys are being pressed. On the server side, I only get an error message if I am pressing keys on the client side.
    More client code:
    Java Code:
    objectOut.reset();
    objectOut.writeObject(seriously);
    The code on the server side is as follows.
    Server code:
    Java Code:
    socket = newsocket;
    seriouslike = new SerializedClass();
    writer = new PrintWriter(socket.getOutputStream(), true);
    objectReader = new ObjectInputStream(socket.getInputStream());
    And within the thread I am calling the following...
    More server code:
    Java Code:
    try {
    
                        seriouslike = (SerializedClass) objectReader.readObject();
                        System.out.println(seriouslike.string);
                        
                        if(!Controls.pressedKeys.isEmpty())
                        {
                            System.out.println(Controls.pressedKeys.toString());
                        }
                    
                    } catch (ClassNotFoundException e) {
    
                        System.out.println("Server couldn't identify object class from client.");
                    }
    And I know it is hitting the 'ClassNotFoundException e' because the console on the server side is printing out "Server couldn't identify object class from client' every time I press a key on the client side, and it is not printing out the string (seriouslike.string) that should be received as part of the serialized object on the server side, either.

    'SerializedClass' is the same file on both Client and Server sides, as well.

    Thanks in advance!
    Last edited by L337CodeMonkey; 02-06-2011 at 09:20 AM.

  2. #2
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    Show e.printStackTrace();
    Last edited by codesmuggler; 02-07-2011 at 09:54 PM.

  3. #3
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Ah... I found out what the problem was. My brother wrote in a line that printed something out (perhaps what you suggested?), and we found that it could not identify the class client.SerializedClass (because the server's version was server.SerializedClass) So, I just needed to create a shared package to put SerializedClass into within NetBeans. (now they both call on shared.SerializedClass, instead)

    Thanks for your help!

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Glad that you solved your problem. But it isn't clear that you are flushing objectOut after every write. You need to do that or you'll probably end up with nasty desrialization problems that crop up intermittently.

    Object serialization across sockets is such a recurring theme here, maybe we need a sticky that explains the right way to do it. IMHO, it's bad practice to directly serialize objects into a socket, because a socket read is not guaranteed to read fully, and so it's possible to get deserialization exceptions randomly. The 'right' way (aka *my* way ;p ) is to serialize into a byte array, send an integer first that hold the number of bytes in the packet, then send the packet, and flush. On the receive side, reasd the integer size first, and then read the packet into a byte array using 'readFully'. This is pretty easy using a DataOutputStream on the send side and a DataInputStream in the receive side....or you can use a loop on the receive side to ensure ebery single byte has been read prior to deserializing.

    Java Code:
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(mySerializableObject);
    
    byte[] bytes = bos.toByteArray();
    
    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    
    dos.writeInt(bytes.length);
    dos.write(bytes);
    dos.flush();
    ...on the reader side

    Java Code:
    DataInputStream dis = new dataInputStream(socket.getInputStream());
    
    int length = dis.readInt();
    
    byte[] bytes = new byte[length];
    
    dis.readFully(bytes);
    
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream(bis);
    Object obj = ois.readObject();

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2010, 06:47 PM
  2. Replies: 17
    Last Post: 10-14-2010, 11:26 PM
  3. Prevent default write object serialization
    By nwboy74 in forum Advanced Java
    Replies: 5
    Last Post: 06-10-2010, 11:30 PM
  4. Object serialization
    By sky in forum New To Java
    Replies: 15
    Last Post: 11-23-2009, 02:10 PM
  5. Serialization/Deserialization Error
    By andrepezzo in forum Advanced Java
    Replies: 2
    Last Post: 12-16-2008, 06:36 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
  •