2015-01-07 14 views
0

我有兩個類,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; 
    } 
} 
+0

什麼是'StreamObject'?另外,服務器是做什麼的? – immibis

+0

這是我創建的一個類,它使用套接字發送和接收對象,我已經添加了您想要的類 –

+0

您測試了接收到的對象的內容嗎?鑑於你沒有重寫toString方法,我不確定你的期望是什麼? – MadProgrammer

回答

0

utilisateur類沒有重載toString,所以它使用默認的實現,它返回類名和哈希碼。

添加這樣的事情utilisateur

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

現在我有兩個單獨的項目,但我想從一個項目發送一個對象到另一個!例如'utilisateur',所以我在兩個項目中創建了相同的類來發送它,並從客戶端本地項目接收它到服務器,那麼完成它的條件是什麼! –