【问题标题】:Socket Connection Timeout套接字连接超时
【发布时间】:2011-05-11 20:19:39
【问题描述】:

在下面的代码中。

如果timeout值为0 (newSocket(Address, 7010, 0); 等待时间是“以毫秒为单位的总时间 =1024” 如果 timout 值为 1 (newSocket(Address, 7010, 1); 等待时间为“Total Time in MilliSeconds =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;
    }
}

【问题讨论】:

  • 您可能应该编辑您的标题以不全部大写。此外,您的问题中有一些代码未格式化为代码。
  • 您必须重新表述您的问题以使您的问题足够清晰。比如把这些事情说清楚;你在这里有什么顾虑?你的期望是什么?你得到了什么结果?

标签: java sockets networking timeout


【解决方案1】:

默认连接超时因平台而异。它大约是 55-75 秒,但它会有所不同。在 Windows 上,它由注册表项控制。你为什么要改变它?如果您正在编写代码,为什么不能总是使用正连接超时?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-17
    • 2011-02-05
    • 2018-11-26
    • 2020-06-02
    • 2016-10-02
    • 2016-02-14
    相关资源
    最近更新 更多