新手上路,学习socket碰到关闭问题,解决好久找到出错问题。
OutputStream os = socket.getOutputStream();
os.close();
InputStream is = socket.getInputStream();
is.close();
这样关闭会把socket一块关闭。报如下错误:
java.net.SocketException: Socket is closed
at java.net.Socket.getInputStream(Socket.java:903)
at test.SoccketClientTest.main(SoccketClientTest.java:21)
不能直接使用clase方法关闭,应使用shutdownOutput或shutdownInput方法,关闭对应的流。
OutputStream os = socket.getOutputStream();
socket.shutdownOutput();
InputStream is = socket.getInputStream();
socket.shutdownInput();