我们如何在客户端套接字上区分服务器套接字连接的异常? [英] How can we differetiate the exceptions for server socket connection close on a client socket?

查看:250
本文介绍了我们如何在客户端套接字上区分服务器套接字连接的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想区分客户端套接字上的服务器连接关闭的异常,例如服务器套接字由于空闲超时而关闭,因为必须在客户端生成原因作为空闲超时异常。同样用于连接失败的连接失败异常等。请帮我解决这个问题。在此先感谢。

I want to differentiate the exceptions for server connection close on a client socket like if the server socket closes due to idle timeout that reason has to be generated on Client side as idle time out exception. Similarly for connection failure as connection failure exception and so on. Please help me to resolve this problem. Thanks in advance.

推荐答案

如果服务器正确关闭套接字,客户端只有一个可能的例外,而不是一个系列客户必须捕捉和弄清楚的不同例外情况。只有一个EOS指示:read()返回-1,readLine()返回null,readXXX()为任何其他X抛出EOFException。

If the server closes the socket properly, there is only one possible exception at the client, not a series of different exceptions that the client has to catch and figure out. There is just an EOS indication: read() returns -1, readLine() returns null, readXXX() throws EOFException for any other X.

如果连接断开由于网络故障,客户端可能会使用文本连接重置获取IOException。如果连接保持不变但服务器没有发送数据且客户端有读取超时,则客户端将获得SocketTimeoutException。这些都不意味着服务器已关闭连接(尽管服务器可能会导致重置连接,我不会在此处记录)。

If the connection is dropped due to a network fault, it is possible for the client to get an IOException with the text 'connection reset'. If the connection stays up but the server isn't sending data and the client has a read timeout, the client will get a SocketTimeoutException. Neither of these implies that the server has closed the connection (although it is possible for the server to cause a connection reset, by means which I will not document here).

如果您需要知道服务器关闭套接字的原因,它必须通过消息告诉您。

If you need to know why the server closed the socket, it will have to tell you via a message.

这篇关于我们如何在客户端套接字上区分服务器套接字连接的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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