簡體   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