情况如下:我有一个 ServerSocket ss,和“Socket socket = ss.accept();”,那么如果我这样做:
istream = socket.getInputStream();
ostream = socket.getOutputStream();
in = new BufferedReader(new InputStreamReader(istream));
out = new PrintWriter(new BufferedOutputStream(ostream));
/*
I use in/out few times
everything OK
*/
ObjectOutputStream oos = new ObjectOutputStream(ostream);
oos.writeObject(someobject);
/* probably code that solves the problem */
String line = in.readLine();
在客户端,我有以下代码:
PrintWriter out = new PrintWriter(new BufferedOutputStream(socket.getOutputStream()),true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
/*
using in/out, no problems
*/
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
SomeObject so = (SomeObject)ois.readObject();
out.println("some text");
一切都很好,直到我发送了一些对象。客户正确接收对象,那里没有问题。但是我不能再使用套接字了。如果我执行 oos.close(),我会得到显示“套接字已关闭”的异常。如果我执行 oos.reset() 我会收到带有类似消息的异常。“套接字重置”。所以我该怎么做?writeObject() 之后是否可以使用相同的输入和输出流?
当我发送“一些文本”时会发生什么,无论我调用 readLine() 多少次,我都会得到空值,我永远不会得到那个“一些文本”。