有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

sockets socketchanneljava。网ConnectException:连接超时:连接

我们的应用程序“有时”超时,我们有一个Java客户端连接到一个unix守护进程,出于某种原因,它不时抛出以下错误:

SocketException: Cannot establish connection to daemon
java.net.ConnectException: Connection timed out: connect
    at sun.nio.ch.Net.connect(Native Method)
    at sun.nio.ch.SocketChannelImpl.connect(Unknown Source)
    at java.nio.channels.SocketChannel.open(Unknown Source)
    at ....
    at java.lang.Thread.run(Unknown Source)

此堆栈跟踪来自以下代码:

  try
  {
    InetSocketAddress inetAddress = new InetSocketAddress(InetAddress.getByName(serverName), serverPort);
    socketChannel = SocketChannel.open(inetAddress); // <--- I think the problem is here
    pipeSck = socketChannel.socket();

  }
  catch (NoRouteToHostException e)//when the remote host cannot be reached or connection was refused
  {
    System.err.println("NoRouteToHostException: Cannot establish connection to daemon");
    e.printStackTrace();
    return 1; /* reply only with error */
  }
  catch (SocketException e)//when the remote host cannot be reached or connection was refused
  {
    System.err.println("SocketException: Cannot establish connection to daemon");
    e.printStackTrace();
    return 1; /* reply only with error */
  }

这在99%的情况下有效。。。知道我们为什么要暂停吗

谢谢


共 (2) 个答案

  1. # 1 楼答案

    “连接超时”表示网络暂时关闭(这是最有可能的情况),或者,如果它没有在Windows上运行,则表示服务器的套接字积压队列已满,这可能在极端负载或拒绝服务攻击下发生

  2. # 2 楼答案

    我同意Crollster的观点,不过您也可以检查一下主机上是否有可能导致问题的防火墙。UNIX软管是否有防火墙?有中间防火墙吗?UNIX主机是否在本地LAN上?如果没有,您是否有不稳定的互联网接入?社交很有趣。:)