Ismail Addou
Ismail Addou

Reputation: 393

objects don't deserializing when i use socket in Java

I have two classes , utilisateur ( means user in french ) and Envellope ( wich means envelope ), so i have many classes to organize sending and receiving objects to/from two classes in localhost ! I want to print the result in the screen after sending and receiving. I conclude that it's not deserializing and the output of toString is a kind of hashcode like this @14ae5a5

Envellope class:

public class Envellope<T> implements Serializable{
    private static final long serialVersionUID = -5653473013975445298L;
    public String todo;
    public T thing;
public Envellope() {
}

public Envellope(String todo, T thing) {
    this.todo = todo;
    this.thing = thing;
}
}

Utilisateur class:

public class utilisateur implements Serializable{
    private static final long serialVersionUID = -5429001491604482315L;
    public String login;
    public String mdp;

    public utilisateur(String l,String m){
        login=l;
        mdp=m;
    }

    public utilisateur(){}
}

and there is the main (Client):

public static void main(String[] args) {
        try {
            Socket socket=new Socket("localhost",4444);
            StreamObject so=new StreamObject(socket);
            Envellope<utilisateur> toSend=new Envellope<utilisateur>("Authenticate",new utilisateur("addou","ismail"));
            so.send(toSend);//sending to ServerSocket 
            Envellope<utilisateur> env=(Envellope<utilisateur>) so.receive();//receiving from server
            System.out.println(env.todo+" Object: "+env.thing);
        } catch (IOException ex) {
            Logger.getLogger(Aaa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

I didn't write here the other classes, because i think it works , but if you need it just tell me !

StreamObject class:

public class StreamObject extends IOS{
    private ObjectOutputStream oos;
    private ObjectInputStream ois; 

    public StreamObject(Socket s) throws IOException{
        super();

            super.os=s.getOutputStream();
            super.is=s.getInputStream();
            oos=new ObjectOutputStream(os);
            ois=new ObjectInputStream(is);

    }

And IOS class is just inputStream and OutputStream ! public void send(Object object) { try { oos.writeObject(object); } catch (IOException e) { System.out.print("Erreur receive socket: "); System.err.print("IOException "); System.out.println(e.getMessage()); } }

    public Object receive() {
        try {
            return ois.readObject();
        } catch (ClassNotFoundException e) {
            System.out.print("Erreur receive socket: ");
                        System.err.print("ClassNotFoundException ");
                        System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.print("Erreur receive socket: ");
                        System.err.print("IOException ");
                        System.out.println(e.getMessage());
        }
        return null;
    }
}

Upvotes: 0

Views: 218

Answers (1)

Your utilisateur class does not override toString, so it uses the default implementation, which returns the class name and hash code.

Add something like this to utilisateur:

@Override
public String toString() {
    return "login="+login+" & mdp="+mdp;
}

Upvotes: 0

Related Questions