繁体   English   中英

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

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

我有一个在Windows XP和Windows 7上运行的Java应用程序。此应用程序与另一台计算机有一个开放的TCP连接。

现在说明一点:在XP机器上,通过返回-1或SocketException,取出网络电缆对我的inputstream.read方法几乎产生了影响。 我如何检测网络连接丢失是一回事。 在Win7机器上,当网络电缆被拔出时,Windows自身会在任务栏的连接图标中告诉我,但我的java应用程序没有得到任何暗示。 它似乎没有发生任何事情。

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;
  }
}

断开后约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.

断开连接后Windows 7的结果是:......没有。 应用程序在没有断开检测的情

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

@edit 2013-02-01 10:59:发生在32位和64位java。 发生在Win7笔记本电脑和普通PC上。

@edit 2013-02-01 12:40:代码示例和结果添加。

@edit 2031-02-06:由于我没有在互联网上找到任何内容,也没有人有解决方案,我为我们的应用程序添加了一个活跃的乒乓机制。 现在我可以在最多20秒后检测到。 连接断了。

@edit 2013-02-08:另一种了解状态的方法是'netsh interface ip show interfaces',但是在知道状态之前必须在java中解析该命令行结果。 也许有更好的方法来获取这些信息。

Win7上的TCP / IP与XP上的实现不同。 一个重要的问题是它包含了一些他们称之为自动调整的东西。 它旨在优化吞吐量等,但可能会出现意外的副作用,将连接状态更改传递给JVM。

查看此处的主题以获取更多信息,包括如何关闭它: 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