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

objects don't deserializing when i use socket in Java

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

信封 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;
}
}

实用主义者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(){}
}

还有主要的(客户端):

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

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

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

    }

而IOS class 只是 inputStream 和 OutputStream ! public无效发送(对象对象){ 尝试 { oos.writeObject(对象); } 赶上(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;
    }
}

您的 utilisateur class 不会覆盖 toString,因此它使用默认实现,即 returns class 名称和哈希码。

utilisateur 添加这样的内容:

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