在Java中使用套接字时,对象不反序列化

时间:2015-01-07 03:05:17

标签: java sockets

我有两个类,utilisateur(意思是法语用户)和Envellope(意思是信封),所以我有很多类来组织发送和接收localhost中两个类的对象! 我想在发送和接收后在屏幕上打印结果。 我得出结论,它不是反序列化,而toString的输出是一种像这样的哈希码@ 14ae5a5

Envellope课程:

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类:

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(){}
}

并且有主要(客户):

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);
        }
    }

我没有在这里写其他课程,因为我认为它有效,但如果你需要它,请告诉我!

StreamObject类:

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);

    }

而IOS类只是inputStream和OutputStream!         public void send(Object object){             尝试{                 oos.writeObject(对象);             } 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;
    }
}

1 个答案:

答案 0 :(得分:0)

您的utilisateur类不会覆盖toString,因此它使用默认实现,它返回类名和哈希码。

将此类内容添加到utilisateur

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