2013-08-20 16 views
0

客户端在GUI中有按钮,当客户端按下按钮时,两个ArrayList被发送到服务器。服务器如何分离它们?如何发送一个列表我已经找到这篇文章,但我怎么能分开两个不同的ArrayList在服务器? Sending an ArrayList<String> from the server side to the client side over TCP using socket?通过套接字发送两个列表

+3

有很多种方法。例如。 1)将两个列表封装到一个对象中,发送该对象。 2)使用两个插座,每个列表一个。 –

回答

1

当使用ObjectOutputStreamObjectInputStream时,它们会抽象协议,以便为您自动“分离”对象。

您只需发送数组列表; .writeObject(Object o);

myObjectOutputStream.writeObject(myArrayList1); 
myObjectOutputStream.writeObject(myArrayList2); 

然后接收它们; readObject();

myArrayList1 = (ArrayList<String>)myObjectInputStream.readObject(); 
myArrayList2 = (ArrayList<String>)myObjectInputStream.readObject(); 

只要确保您按照它们发送的顺序读取它们即可。


作为一个方面不行,一定要在ObjectOutputStream打电话.reset()如果你正在写一个更新的对象流,因为它有某种形式的缓存来保存它重新发送已经被写入对象流。