Does closing the inputstream of a socket also close the socket connection?

前端 未结 1 658
天涯浪人
天涯浪人 2020-12-08 14:28

In Java API,


Socket socket = serverSocket.accept();
BufferedReader fromSocket = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWr         


        
1条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 15:01

    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(); 
    

    0 讨论(0)
提交回复
热议问题