2010-11-18 173 views
1

在下面的代碼中。套接字連接超時

如果timout值爲0(newSocket(地址,7010,0); 等待時間爲 「總時間以毫秒爲單位= 1024」 如果timout值爲1(newSocket(地址,7010,1); 等待時間「總時間(毫秒)= 22」

是否有默認的操作系統設置(Windows),它可以減少等待時間超時值爲「0」。試了幾個註冊表項LmhostsTimeoutTcpTimedWaitDelay沒有成功。請幫我解決這個問題。


import java.net.*; 
import java.io.*; 

public class TestConnection 
{ 
public static void main (String a[]) 
{ 
long t1 = System.currentTimeMillis(); 
      try 
      { 
        InetAddress Address = InetAddress.getLocalHost(); 
        System.out.println("Host Address" + Address + " Port " + 7010); 
        newSocket(Address, 7010, 0); 
     long t2 = System.currentTimeMillis(); 
     System.out.println("SenthilTS1=" + (t2-t1)); 

    }catch (Exception e) 
      { 
    long t2 = System.currentTimeMillis(); 
    System.out.println("Total Time in MilliSeconds =" + (t2-t1)); 
    //    e.printStackTrace(); 
      } 
} 

    /*package*/ static void initSocket(Socket sock) throws SocketException { 
     try { 
     sock.setTcpNoDelay(true); 
     } catch (SocketException se) { 
     try { sock.close(); } catch (IOException ignore) {} 
     //CR283953. Differentiate that the exception is thrown while doing a 
     //socket set operation. 
     throw se; 
     } 
    } 

     static Socket newSocket(InetAddress address, int port, 
          int timeout) throws IOException 
    { 
     Socket sock = new Socket(); 
     initSocket(sock); 
     InetSocketAddress ina = new InetSocketAddress(address, port); 
    System.out.println("******** SocketMuxer.newSocket before Socket.connect() call TimeStamp (ms)=" + System.currentTimeMillis()); 
    try{ 
    sock.connect(ina, timeout); 
    System.out.println("******** SocketMuxer.newSocket after connect() SUCCESS call TimeStamp (ms)=" + System.currentTimeMillis()); 
    }catch (IOException e) 
    { 
    System.out.println("******** SocketMuxer.newSocket after connect() FAILED call TimeStamp (ms) =" + System.currentTimeMillis()); 
    e.printStackTrace(); 
    throw e; 
    } 
    return sock; 
    } 
} 
+1

你應該編輯你的標題不要全部大寫。此外,您的問題中還有一些代碼未格式化爲代碼。 – 2010-11-18 05:20:23

+0

你必須改變你的問題,以使你的問題清楚。比如說清楚這些事情;你在這裏擔心什麼?你的期望是什麼?你得到的結果是什麼? – 2010-11-18 06:36:27

回答

0

默認連接超時時間因平臺而異。它大約55-75秒,但它有所不同。在Windows上,它由註冊表項控制。你爲什麼想改變它?如果您正在編寫代碼,爲什麼不能總是使用正連接超時?