套接字连接超时

发布于 2024-10-03 14:36:35 字数 2127 浏览 0 评论 0原文

在下面的代码中。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他不在意 2024-10-10 14:36:35

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

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文