本帖最后由 何丛 于 2013-9-24 12:29 编辑
- public class DemoClient {
- public static void main(String[] args) {
- try {
- Socket socket = new Socket("localhost" ,10006) ;
-
- //实例化流对象
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())) ;
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())) ;
-
- //向服务器端发送消息
- String string = "hello , this is Client\n " ;
- bufferedWriter.write(string) ;
- bufferedWriter.flush();
- socket.shutdownOutput();
- bufferedWriter.close();
- //接收消息
- System.out.println(bufferedReader.readLine());
- //关闭
- // bufferedWriter.close();
- bufferedReader.close();
- socket.close();
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
复制代码 在使用完BufferedWriter后关闭它,会在System.out.println(bufferedReader.readLine());这一句报异常socket closed , 如果bufferedWriter.close();放在System.out.println(bufferedReader.readLine());之后则运行正常,请问Socket流关闭之后Socket也随之关闭了吗?那为什么socket.close();不报错呢?
|