2015-12-28 128 views
0

我对ObjectInputStream有问题。我想通过这个发送对象,但程序停止在我想初始化ObjectInputStream的位置。我已经搜索了回答,并且我发现在你初始化一个ObjectInputStream之前需要打开ObjectOutputstream。但是这在那里是有保证的。ObjectInputStream没有初始化

我的客户端类的代码段:

socket = new Socket(InetAddress.getByName(server).getHostAddress(), 13340); 
     messages = new Scanner(socket.getInputStream()); 
     p = new PrintStream(socket.getOutputStream()); 
     ObjectOutputStream o = new ObjectOutputStream(socket.getOutputStream()); 

     p.println("addServerContent"); 

     o.flush(); 
     System.out.println("1"); 
     o.writeObject(String.valueOf(index)); 
     o.writeObject(s); 

而且继承人的服务器的一部分:

}else if(what.equals("addServerContent")){ 
     ObjectInputStream i = null; 
     try{ 
      i = new ObjectInputStream(s.getInputStream()); 
     }catch(IOException e){} 

     while(GxMS2.ListUsed){ 
      try { 
       Thread.sleep(10); 
      } catch (InterruptedException ex) {} 
     } 

     try{ 

      System.out.println("1"); 
      int index = Integer.parseInt((String)i.readObject()); 
      ServerContent sc = (ServerContent)i.readObject(); 

在服务器它doesn't甚至达到了 “1” 的标志。

为什么它不工作?

谢谢

+0

它是挂起这里'而(GxMS2.ListUsed)'或其他地方... – Tom

+0

不,它不存在,我在同时前面加一个标志,它没有被触发 – RPanic

+0

“我发现,用ObjectOutputStream需求被打开之前,你可以初始化一个ObjectInputStream“绝对不是真的 – ControlAltDel

回答

0

你不能用一个对象流结合字符流,以便其既可以是您用PrintWriter或与ObjectOutputStream但不能同时包裹OutputStream

+0

为什么不呢?他们互相阻挡? – RPanic