【发布时间】: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