SocketException 连接重置
我创建了一个接受 TCP 连接的服务器。连接到套接字后,它循环从输入流读取数据。
步骤:
- 我启动了我的服务器。
- 启动客户端。
现在我正在关闭客户端。
然后服务器给我 SocketException Connection Reset
在从输入流读取之前如何检查我的客户端是否处于活动状态。
I created a server which accept TCP connection . After connected to socket it looping for reading data from input stream.
Steps:
- I started my server.
- Start Client.
Now I am closing client.
Then Server gives me SocketException Connection Reset
How do I check that my client is alive or not before reading from input stream.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的服务器收到“连接重置”,它可能正在写入已被另一端关闭的连接。用户按下“后退”按钮的浏览器就是一个很好的例子。如果这是预期情况,请忽略该异常。如果构成应用程序协议错误,请调试应用程序。
If your server gets a 'connection reset' it is probably writing to a connection that has already been closed by the other end. A browser whose user presses the 'back' button is a good example. If this is an expected condition, ignore the exception. If it constitutes an application protocol error, debug the application.
有两种方法可以检查套接字是否已连接,您可以读取或写入它,如果已连接,则不会收到错误,如果未连接,则会收到错误。这就是我检查套接字是否已连接的方式:
当套接字连接时,读取器会等待,直到有东西要读取(message = reader.readLine())。当客户端断开连接并且 Socket 关闭时,读取器会抛出异常,因为流已关闭,因此没有任何内容可以读取。我希望这有帮助!
There are 2 ways you can check if a socket is connected, you can either read or write to it, if it is connected you wont get an error, if it isnt connected you will get an error. This is how i check if a socket is connected:
When the Socket is connected the reader waits untill there is somthing to be read(message = reader.readLine()). When the client disconnects and the Socket is closed, the reader throws an exception because there is nothing that can be read because the stream is closed. I hope this helps!