2012-02-02 90 views
0

我正在尝试使用套接字流进行多个数据传输。插座上的多个输出

  • 用于发送文字
  • 发送List<>
  • ...

我可以只使用一个插座,用于发送String,告诉客户什么样的数据,以期待的,其次由预期的数据(在这种情况下为List<>)?

这里就是我想的是:

//declarations 
//outputs and inputs 
    private ObjectInputStream input; 
    private ObjectOutputStream output; 
    private OutputStream checkStatus; 
    private PrintWriter out; 
    ... 
    private void forwardMessage(List<User> clients) throws IOException { 
     checkStatus = client.getOutputStream(); 
     out = new PrintWriter(new OutputStreamWriter(checkStatus), true); 
     out.println("Command Option 1");  
     client.shutdownOutput(); 
     output = new ObjectOutputStream (client.getOutputStream()); 
     for (int i = 0; i < clients.size(); i++) {  
      output.flush(); 
      output.writeObject(clients.get(i)); 
      output.flush(); 
      output.reset(); 
     } 
     output.writeObject(null); 
     client.shutdownOutput(); 
    } 

给予当我尝试这是“java.net.SocketException: Socket is closed

我也试图关闭的OutputStream但给出相同的结果误差。

我怎样才能使用一个流套接字进行多个数据传输?

+2

why client.shutdownOutput(); getOutputStream()之前? – kosa 2012-02-02 19:23:27

回答