来自 Socket
文档:
为了测试服务器中客户端之间的交互,我写了一些客户端机器人。 这些机器人生成一些随机的客户端请求。由于它们只需要向服务器进行写操作,所以它们不需要输入流,也不需要读取服务器发送的更新。以下是机器人主体代码:shutdownInput
public void shutdownInput() throws IOException
Places the input stream for this socket at "end of stream". Any data sent to the input stream side of the socket is acknowledged and then silently discarded.
If you read from a socket input stream after invoking shutdownInput() on the socket, the stream will return EOF.
private void runWriteBot(PrintWriter out) throws IOException {
//socket.shutdownInput();
String request;
System.out.println("Write bot ready.");
while (!quit) {
request = randomRequest();
out.println(request);
sleep();
}
}
如果我取消注释
shutdownInput
,服务器的客户端处理程序将抛出异常:
我没想到在套接字的另一侧会抛出异常。文档(至少对我而言)表明,由另一方发送的任何内容都将被静默地丢弃,不会对另一端的活动造成干扰,即不会导致对方抛出异常。连接重置
我可以忽略服务器发送的内容吗,还是应该排空输入流?
是否有自动处理方式,还是需要定期读取并忽略?