Results 1 to 1 of 1
  1. #1
    JDaniel is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default Client/Server Serialization Problem

    Hi, I'm writting a client / server instant message application using sockets. Because I'm implementing a simple comunication protocol in application layer, I need to peform the comunication using objects. Few days ago I have encountered a serious problem with serialization which i cannot solve. Here it is:

    Server:

    The server gets input form client and echos it back.

    //Streams declaration

    private ObjectInputStream in = null;
    private ObjectOutputStream out = null;

    //Definition in constructor

    out = new ObjectOutputStream(socket.getOutputStream());
    in = new ObjectInputStream(socket.getInputStream());

    //Code in run(),

    Java Code:
    try {
                String str = "";
                ArrayList<String> list = new ArrayList<String>();
                list.add(str);
                NetworkData data = new NetworkData(0, list);
                while(true)
                {
                    [COLOR="Red"]data = (NetworkData)in.readObject();[/COLOR]
                    if(data.getAtributeList().get(0).equals("end"))
                        break;
                    System.out.println("Echoing " + data.getAtributeList().get(0));
                    //
                    out.writeObject(data);
                    out.flush();
                }
            }
            catch(ClassNotFoundException e){
            } catch (IOException e) {
            } finally {
                try {
                    socket.close();
                } catch (IOException e) {
                }
            }
    Client:

    The client gets input from keyboard, creates an object and tries to send it to serwer. Then gets response and prints it.

    //Streams declaration

    private ObjectOutputStream out = null;
    private ObjectInputStream in = null;

    //Definition in class constructor

    in = new ObjectInputStream(socket.getInputStream());
    out = new ObjectOutputStream(socket.getOutputStream());

    //The code in run() method (class extends Thread)

    Java Code:
     String str = stdin.readLine();
                ArrayList<String> list = new ArrayList<String>();
                list.add(str);
                NetworkData outdata = new NetworkData(0, list);
    
                while (!outdata.getAtributeList().get(0).equals("end")) {
                    [COLOR="red"]out.writeObject(outdata);[/COLOR]
                    out.flush();
                    NetworkData indata = (NetworkData) in.readObject();
                    System.out.println(indata.getAtributeList().get(0));
                    //Clear list now
                    list.clear();
                    //Get new input from keyboard
                    str = stdin.readLine();
                    //Update list (data has reference to list)
                    list.add(str);
                }
    So the client gets input from keyboard, creates an object and tries to send it to serwer. Then gets response and prints it.

    Here is the serialized class:


    Java Code:
    import java.io.Serializable;
    import java.util.ArrayList;
    
    /**
     * Object representing an information being send via network
     * @author Daniel
     */
    public class NetworkData implements Serializable{
    
        /**Operation code*/
        Integer code = null;
        /**Atributes*/
        ArrayList<String> atributeList = null;
    
        NetworkData(Integer code, ArrayList<String> list)
        {
            this.code = code;
            this.atributeList = list;
        }
    
        /**
         * Get atributes
         * @return atributes list
         */
        public ArrayList<String> getAtributeList() {
            return atributeList;
        }
    
        /**
         * Set atributes
         * @param atributeList
         */
        public void setAtributeList(ArrayList<String> atributeList) {
            this.atributeList = atributeList;
        }
    
        /**
         * Get code
         * @return code
         */
        public Integer getCode() {
            return code;
        }
    
        /**
         * Set code
         * @param code
         */
        public void setCode(Integer code) {
            this.code = code;
        }
    
    }
    The problem concerns the comunication in client/serwer methods in.readObject(), out.writeObject() - indicated with red color. Every time one of them throws an exception which falls into finally clause. I had spend a lot of time on searching for error, unfortunately i didn't find it. Moreover when i switch the comunication using BufferedReader and PrintWriter streams (and send information with readlLine(), and printLine() methods) everything works fine. I think it may be some issue with serializing the object. The client and server programs are in different projects. The NetworkData class was copied so i think they are the same in both. Pleas help me if you have any clue.

    I tried this NetworkData class with other program and it works. Don't get it.

    ps. Im a novice in java.

    This code works either:

    Server:

    Java Code:
    while(true)
                {
                    String str = (String)in.readObject();
                    if(str.equals("end"))
                        break;
                    System.out.println("Echoing " + str);
                    out.writeObject(str);
                }
    Client:

    Java Code:
    String str = stdin.readLine();
                while (!str.equals("end")) {
                    out.writeObject(str);
                    str = (String)in.readObject();
                    System.out.println(str);
                    str = stdin.readLine();
                }

    Solved!
    Last edited by JDaniel; 12-18-2010 at 08:24 PM.

Similar Threads

  1. about client to server problem
    By raywai in forum New To Java
    Replies: 3
    Last Post: 05-13-2010, 05:34 PM
  2. client server problem
    By kievari in forum Advanced Java
    Replies: 4
    Last Post: 02-12-2010, 09:06 AM
  3. Simple server/client text problem
    By Singing Boyo in forum New To Java
    Replies: 5
    Last Post: 06-02-2009, 11:33 AM
  4. TCP server-client and serialization
    By eggmanpete in forum Advanced Java
    Replies: 3
    Last Post: 02-20-2009, 07:04 PM
  5. client-server communication problem
    By revathi17 in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 03:21 PM

Posting Permissions

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