【问题标题】:objects don't deserializing when i use socket in Java当我在 Java 中使用套接字时,对象不会反序列化
【发布时间】:2015-03-04 20:07:46
【问题描述】:

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

信封类:

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 ! 公共无效发送(对象对象){ 尝试 { oos.writeObject(对象); } 捕捉(IOException e){ System.out.print("错误接收套接字:"); 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;
    }
}

【问题讨论】:

  • 什么是StreamObject?另外,服务器是做什么的?
  • 这是我创建的一个类,它通过套接字发送和接收对象,我已经添加了你想要的类
  • 你测试过接收对象的内容吗?鉴于您没有覆盖 toString 方法,我不确定您在期待什么?
  • 是的!我认为它是一种哈希码,它给出了下一个结果:@14ae5a5

标签: java sockets


【解决方案1】:

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

utilisateur添加类似这样的内容:

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

【讨论】:

  • 现在我有两个独立的项目,但我想将一个对象从一个项目发送到另一个!例如utilisateur,所以我在两个项目中创建了相同的类,从客户端本地项目发送和接收它到服务器一,实现它的条件是什么!
猜你喜欢
  • 2012-09-02
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 2020-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多