简体   繁体   English

java应用程序tcp连接丢失检测在Windows 7中有所不同?

[英]java application tcp connection loss detect differs in windows 7?

I have a java application running on Windows XP and also on Windows 7. This application has an open tcp connection to another computer. 我有一个在Windows XP和Windows 7上运行的Java应用程序。此应用程序与另一台计算机有一个开放的TCP连接。

Now comes the point: On an XP machine unpugging the network cable has a nearly immidiate effect on my inputstream.read method by returning -1 or SocketException. 现在说明一点:在XP机器上,通过返回-1或SocketException,取出网络电缆对我的inputstream.read方法几乎产生了影响。 That's one thing how I detect network connection loss. 我如何检测网络连接丢失是一回事。 On the Win7 machine when the network cable is unplugged windows itself tells me this in its connection icon in the task bar but my java application doesn't get any hint of this. 在Win7机器上,当网络电缆被拔出时,Windows自身会在任务栏的连接图标中告诉我,但我的java应用程序没有得到任何暗示。 It stays as if nothing happened. 它似乎没有发生任何事情。

Does TCP behaves differently on windows 7 or does windows7 shield this information from its applications? TCP在Windows 7上的行为是否不同,或者Windows7是否将此信息与其应用程序屏蔽开来?

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 is after disconnection is: 断开后约5秒后Windows xp上的结果是:

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 disconnection is: ... nothing. 断开连接后Windows 7的结果是:......没有。 Application stays open without disconnect detection. 应用程序在没有断开检测的情

@edit 2013-02-01 09:54: Happens in jre6 and jre7. @edit 2013-02-01 09:54:发生在jre6和jre7。

@edit 2013-02-01 10:59: Happens in 32bit and 64bit java. @edit 2013-02-01 10:59:发生在32位和64位java。 Happens on Win7 Laptop and normal PC. 发生在Win7笔记本电脑和普通PC上。

@edit 2013-02-01 12:40: Code example and result added. @edit 2013-02-01 12:40:代码示例和结果添加。

@edit 2031-02-06: Since I found nothing on the internet and nobody had a solution for this, I added an active ping-pong mechanism to our applications. @edit 2031-02-06:由于我没有在互联网上找到任何内容,也没有人有解决方案,我为我们的应用程序添加了一个活跃的乒乓机制。 Now I can detect after at most 20 sec. 现在我可以在最多20秒后检测到。 that the connection is broken. 连接断了。

@edit 2013-02-08: Another way to find out about the status is 'netsh interface ip show interfaces' but that command line result must be parsed in java before knowing the status. @edit 2013-02-08:另一种了解状态的方法是'netsh interface ip show interfaces',但是在知道状态之前必须在java中解析该命令行结果。 Maybe there is a better way to get that information. 也许有更好的方法来获取这些信息。

TCP/IP on Win7 is different than the implementation on XP. Win7上的TCP / IP与XP上的实现不同。 One important issue is that it includes something they call Auto-Tuning. 一个重要的问题是它包含了一些他们称之为自动调整的东西。 It's designed to optimize throughput and such, but there could be unintended side-effects for passing connection status changes to the JVM. 它旨在优化吞吐量等,但可能会出现意外的副作用,将连接状态更改传递给JVM。

Check out the thread here for more information including how to turn it off: http://social.technet.microsoft.com/Forums/en-US/w7itpronetworking/thread/1a6197df-ada7-4b12-92b7-a8a2f454f9a3 查看此处的主题以获取更多信息,包括如何关闭它: http//social.technet.microsoft.com/Forums/en-US/w7itpronetworking/thread/1a6197df-ada7-4b12-92b7-a8a2f454f9a3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM