Is java tcp application lost connection detection different in Windows 7?

I have a Java application running on Windows XP as well as Windows 7. This application has an open tcp connection to another computer.

Now comes the point: on an XP machine, disconnecting the network cable has almost negligible effect on my inputstream.read method, returning -1 or a SocketException. This is one thing as I discovered the loss of the network connection. On a Win7 machine, when the network cable is unplugged, windows themselves say this in the connection icon in the taskbar, but my Java application has no hint of it. He remains as if nothing happened.

Does TCP support behavior differently in Windows 7, or does Windows7 protect this information from its applications?

package tcpconnectionlossdetectiontest;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;

public class TcpListener extends Thread {
  private int _portNo = 23456;
  private boolean _done = false;

  public static void main(String[] args) {
    int port = 23456;

    if (args.length > 0) {
      int position = -1;
      do {
        String arg = args[++position].trim();
        if (arg.equalsIgnoreCase("-p") || arg.equalsIgnoreCase("--PortNo")) {
          String argParameter = args[++position].trim();
          // e.g. store additional argument
          try {
            port = Integer.parseInt(argParameter);
          } catch (NumberFormatException nfex) {
            port = 23456;
          }
          System.out.println("Argument " + position + " (" + arg + ", " + argParameter + "): port number set to " + port);
        } else {
          System.out.println("Argument " + position + " (" + arg + ") unknown.");
        }
      }
      while (position + 1 < args.length);
      // Parsing command line arguments ready.
    }

    TcpListener listener = new TcpListener(port);
    listener.start();
  }

  public TcpListener(int portNo) {
    this._portNo = portNo;
  }

  public void run() {
    Socket s = null;
    InputStream is = null;
    byte[] buf = new byte[1000];
    int readResult = 0;
    int maxOpen = 3;
    int openCounter = 0;
    try {
      ServerSocket sSocket = new ServerSocket(this._portNo);
      while (openCounter < maxOpen) {
        if (s == null) {
          try {
            System.out.println("waiting for connection on port " + this._portNo);
            sSocket.setSoTimeout(60000);
            s = sSocket.accept();
            if (s != null) {
              System.out.println("got connection on port " + this._portNo);
              openCounter++;
              is = s.getInputStream();
            }
          } catch (SocketTimeoutException stex) {
            System.out.println("no connection yet...");
          }
        }
        if (s != null && is != null) {
          readResult = is.read(buf, 0, 1000);
        }
        if (readResult == -1) {
          readResult = 0;
          System.out.println("connection broken...");
          is=null;
          if (s != null) {
            s.close();
            s=null;
          }
        } else if (readResult > 0) {
          System.out.println("Data read: " + new String (buf,0,readResult));
        }
      }
    } catch (IOException ioex) {
      System.out.println("IO exception caught:");
      ioex.printStackTrace();
    }
    this._done = true;
  }
}

      

Result on windows xp after round about 5 seconds after disconnecting:

waiting for connection on port 23456
no connection yet...
waiting for connection on port 23456
got connection on port 23456
Data read: Here is a connection from win7 to xp...
Data read: 

IO exception caught:
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:168)
    at tcpconnectionlossdetectiontest.TcpListener.run(TcpListener.java:73)
Process exited with exit code 0.

      

Result on windows 7 after disconnecting: ... nothing. The application remains open without detecting disconnection.

@edit 2013-02-01 09:54: Happens in jre6 and jre7.

@edit 2013-02-01 10:59: Happens in 32bit and 64bit Java. Happens on Win7 laptop and regular PC.

@edit 2013-02-01 12:40: Added sample code and output.

@edit 2031-02-06: Since I didn't find anything online and we didn't have a solution for this, I added an active ping-pong engine to our apps. Now I can detect in no more than 20 seconds. that the connection is broken.

@edit 2013-02-08: Another way to know the status is with the netsh interface ip show, but this command line output must be parsed in java before knowing the status. There is probably a better way to get this information.

+3


source to share


1 answer


TCP / IP on Win7 differs from XP implementation. One of the important issues is that it includes what they call Auto-Tuning. It is intended to optimize throughput, etc., but there might be unintended side effects for pushing connection status changes to the JVM.



Check out this thread for more information, including how to disable it: http://social.technet.microsoft.com/Forums/en-US/w7itpronetworking/thread/1a6197df-ada7-4b12-92b7- a8a2f454f9a3

0


source







All Articles