SocketChannel - java.net.ConnectException:连接超时:连接

时间:2011-11-04 04:04:34

标签: java sockets networking tcp

我们的应用程序“有时”超时,我们有一个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 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我同意Crollster,虽然您也可以检查主机上是否有可能导致问题的防火墙。 UNIX软管是否有防火墙?任何中间防火墙? UNIX主机是否在本地LAN上?如果没有,你有不稳定的互联网接入?网络很有趣。 :)