Java – distinguish between connection rejected and connection timeout

Using Java net. socket. When connect(), both connection rejection and timeout will result in connectexception

java.net.ConnectException: Connection timed out: connect

java.net.ConnectException: Connection refused: connect

How can I safely distinguish between the two? Ensure that parsing the error message can complete the work But when the message changes in future Java versions, I'm not lucky

Bigger picture: I'm writing a web service client using Jax - WS with Metro implementation When a web service call fails, I want to clearly report the reason for the failure so that the problem can be solved quickly

Solution

Unfortunately, in the sun JDK, this information is not provided anywhere except for strings See plainsocketimpl Line 473 of C (for * IX) and net_ util_ Md.c (for Windows)* IX implementations sometimes call net_ Throwbynamewithlasterror (from * IX net_util_md.c), which will contain errno in the string; This feature exists on windows, but it is not used here

So you have to rely on strings and hope they don't change Sun does not seem to localize them, which makes sense because they should not be user - oriented You can try parsing errno for more stable error codes

If there is no string or errno match, you should also ensure that there is a fallback

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>