对象不';在Java中使用socket时不能反序列化
我有两个类,usilizateur(法语中的user)和Envellope(wich表示信封),所以我有很多类来组织向localhost中的两个类发送和接收对象! 发送和接收后,我想在屏幕上打印结果。 我的结论是,它不是反序列化的,toString的输出是一种类似于@14ae5a5的哈希代码 Envellope类:对象不';在Java中使用socket时不能反序列化,java,sockets,Java,Sockets,我有两个类,usilizateur(法语中的user)和Envellope(wich表示信封),所以我有很多类来组织向localhost中的两个类发送和接收对象! 发送和接收后,我想在屏幕上打印结果。 我的结论是,它不是反序列化的,toString的输出是一种类似于@14ae5a5的哈希代码 Envellope类: public class Envellope<T> implements Serializable{ private static final long seri
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;
}
}
主要客户有:
IOS类就是inputStream和OutputStream!
公共无效发送(对象){
试一试{
oos.writeObject(对象);
}捕获(IOE异常){
系统输出打印(“错误接收插座:”;
系统错误打印(“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;
}
}
您的
usilizateur
类不会覆盖toString
,因此它使用默认实现,返回类名和哈希代码
将类似的内容添加到usilizateur
:
@Override
public String toString() {
return "login="+login+" & mdp="+mdp;
}
什么是
StreamObject
?还有,服务器是做什么的?它是我创建的一个类,它通过套接字发送和接收对象,我已经添加了你想要的类。你测试了接收对象的内容了吗?考虑到您没有重写toString方法,我不确定您期望的是什么?是的!我认为这是一种哈希代码,它给出了下一个结果:@14AE5A5现在我有两个独立的项目,但我想将一个对象从一个项目发送到另一个项目!例如,usilizateur
,因此我在两个项目中创建了相同的类,将其从客户端本地项目发送到服务器项目,并将其接收到服务器项目,实现这一点的条件是什么!
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);
}
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;
}
}
@Override
public String toString() {
return "login="+login+" & mdp="+mdp;
}