2010-11-18 223 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上,它由注册表项控制。你为什么想改变它?如果您正在编写代码,为什么不能总是使用正连接超时?