我有兩個類,utilisateur(意味着用戶在法國)和Envellope(這意味着信封),所以我有很多類來組織發送和接收對象到/從兩個類在本地主機! 我想在發送和接收後在屏幕上打印結果。 我斷定它不是反序列化和toString的輸出是一種這樣的hashCode @ 14ae5a5對象不反序列化當我在Java中使用套接字
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);
}
}
我沒寫在這裏t他其他班級,因爲我認爲它的作品,但如果你需要它,告訴我!
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! (object object){ try; 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;
}
}
什麼是'StreamObject'?另外,服務器是做什麼的? – immibis
這是我創建的一個類,它使用套接字發送和接收對象,我已經添加了您想要的類 –
您測試了接收到的對象的內容嗎?鑑於你沒有重寫toString方法,我不確定你的期望是什麼? – MadProgrammer