开发者

Socket Connection Timeout

开发者 https://www.devze.com 2023-01-25 11:17 出处:网络
In the code below. If the timout value is 0(newSocket(Address, 7010, 0); The wait time is \"Total Time in MilliSeconds =1024\"

In the code below.

If the timout value is 0 (newSocket(Address, 7010, 0); The wait time is "Total Time in MilliSeconds =1024" If the timout value is 1 (newSocket(Address, 7010, 1); The wait time is "Total Time in MilliSeconds =22"

Is there any default OS settings (Windows) which can reduce the waiting time for the timeout value '0'. Tried few registry entries LmhostsTimeout, TcpTimedWaitDelay with no success. Please help me in resolving this issue.


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


The default connection timeout varies per platform. It is around 55-75 seconds but it varies. On Windows it is controlled by a registry entry. Why do you want to change it? If you are writing the code, why can't you just always use a positive connect timeout?

0

精彩评论

暂无评论...
验证码 换一张
取 消