Why my Java server close sockets?

后端 未结 1 1838
北海茫月
北海茫月 2020-12-21 14:20

I have server and client. My server acepts all connections and returns to client string. But when I try to send more lines its crashed with

java.net.Soc

相关标签:
1条回答
  • 2020-12-21 14:57

    From the javadoc of getOutputStream() in Socket:

    Closing the returned OutputStream will close the associated socket.

    Also, closing a PrintWriter (and all other Printers/Writers) close their underlying streams as well. So, you are closing your OutputStream by closing the PrintWriter (in hiMsg()) and then trying to write to the socket, which was closed.

    To fix the problem, don't close the PrintWriter. Garbage collection will take care of it for you! But do close the socket when you are done with it.

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