是否关闭套接字的输入流也关闭套接字连接? [英] Does closing the inputstream of a socket also close the socket connection?

查看:130
本文介绍了是否关闭套接字的输入流也关闭套接字连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在的Java API,

In Java API,



Socket socket = serverSocket.accept();
BufferedReader fromSocket = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter toSocket = new PrintWriter(socket.getOutputStream());
//do sth with fromSocket ... and close it
fromSocket.close();
//then write to socket again
toSocket.print("is socket connection still available?\r\n");
//close socket
socket.close();

在上面的code,之后我关闭InputStream fromSocket,似乎插座连接不可用了 - 客户不会收到是socket连接仍然可用的消息。
这是否意味着关闭套接字的输入流也关闭套接字本身呢?

In the above code, after I close the InputStream fromSocket, it seems that the socket connection is not available anymore--the client wont receive the "is socket connection still available" message. Does that mean that closing the inputstream of a socket also closes the socket itself?

推荐答案

是,关闭输入流关闭套接字。您需要使用套接字的shutdownInput方法,以<一个href=\"http://download.oracle.com/javase/1.4.2/docs/api/java/net/Socket.html#shutdownInput%28%29\">close刚输入流:

Yes, closing the input stream closes the socket. You need to use the shutdownInput method on socket, to close just the input stream:

//do sth with fromSocket ... and close it 
socket.shutdownInput(); 

然后,您仍然可以发送到输出插孔

Then, you can still send to the output socket

//then write to socket again 
toSocket.print("is socket connection still available?\r\n"); 
//close socket 
socket.close(); 

这篇关于是否关闭套接字的输入流也关闭套接字连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆