Java detect lost connection [duplicate]

2019-01-04 02:28发布

This question already has an answer here:

When I'm using e.g. PuTTY and my connection gets lost (or when I do a manual ipconfig /release on Windows), it responds directly and notifies my connection was lost.

I want to create a Java program which monitors my Internet connection (to some reliable server), to log the date/times when my internet fails.

I tried use the Socket.isConnected() method but that will just forever return "true". How can I do this in Java?

9条回答
地球回转人心会变
2楼-- · 2019-01-04 02:30

Okay so I finally got it working with

try
{
    Socket s = new Socket("stackoverflow.com",80);
    DataOutputStream os = new DataOutputStream(s.getOutputStream());
    DataInputStream is = new DataInputStream(s.getInputStream());
    while (true)
    {
        os.writeBytes("GET /index.html HTTP/1.0\n\n");
        is.available();
        Thread.sleep(1000);
    }
}
catch (IOException e)
{
    System.out.println("connection probably lost");
    e.printStackTrace();
}

Not as clean as I hoped but it's not working if I leave out the os.writeBytes().

查看更多
叼着烟拽天下
3楼-- · 2019-01-04 02:31

Why not use the isReachable() method of the java.net.InetAddress class?

How this works is JVM implementation specific but:

A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.

If you want to keep a connection open continually so you can see when that fails you could connect to server running the ECHO protocol yourself rather than having isReachable() do it for you and read and write data and wait for it to fail.

查看更多
狗以群分
4楼-- · 2019-01-04 02:33

Even though TCP/IP is "connection oriented" protocol, normally no data is sent over an idle connection. You can have a socket open for a year without a single bit sent over it by the IP stack. In order to notice that a connection is lost, you have to send some data on the application level.(*) You can try this out by unplugging the phone cable from your ADSL modem. All connections in your PC should stay up, unless the applications have some kind of application level keepalive mechanism.

So the only way to notice lost connection is to open TCP connection to some server and read some data from it. Maybe the most simple way could be to connect to some FTP server and fetch a small file - or directory listing - once in a while. I have never seen a generic server which was really meant to be used for this case, and owners of the FTP server may not like clients doing this.

(*) There is also a mechanism called TCP keepalive but in many OS's you have to activate it for all applications, and it is not really practical to use if you want to notice loss of connection quickly

查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-04 02:33

Its probably simpler to connect to yahoo/google or somewhere like this.

    URL yahoo = new URL("http://www.yahoo.com/");
    URLConnection yc = yahoo.openConnection();
    int dataLen = yc.getContentLength() ;

Neil

查看更多
Viruses.
6楼-- · 2019-01-04 02:34

If the client has disconnects properly, a read() will return -1, readLine() returns null, readXXX() for any other X throws EOFException. The only reliable way to detect a lost TCP connection is to write to it. Eventually thus will throw an IOException 'connection reset', but it takes at least two writes due to buffering.

查看更多
\"骚年 ilove
7楼-- · 2019-01-04 02:34

You might want to try looking at the socket timeout interval. With a short timeout (I believe the default is 'infinite timeout') then you might be able to trap an exception or something when the host becomes unreachable.

查看更多
登录 后发表回答