How to avoid a NoRouteToHostException?

后端 未结 6 1789
傲寒
傲寒 2020-12-02 06:09

Disclosure: the code I\'m working on is for university coursework.

Background: The task I\'m trying to complete is to report on the effect of different threading tec

相关标签:
6条回答
  • 2020-12-02 06:15

    For any other Java users that stumble across this question, I would recommend using connection pooling so connections are reused properly.

    0 讨论(0)
  • 2020-12-02 06:22

    This may help:

    The Ephemeral Port Range

    Another important ramification of the ephemeral port range is that it limits the maximum number of connections from one machine to a specific service on a remote machine! The TCP/IP protocol uses the connection's 4-tuple to distinguish between connections, so if the ephemeral port range is only 4000 ports wide, that means that there can only be 4000 unique connections from a client machine to a remote service at one time.

    So maybe you run out of available ports. To get the number of available ports, see

    $ cat /proc/sys/net/ipv4/ip_local_port_range 
    32768   61000
    

    The output is from my Ubuntu system, where I'd have 28,232 ports for client connections. Hence, your test would fail as soon as you have 280+ clients.

    0 讨论(0)
  • 2020-12-02 06:28

    Have you tried setting:

    echo "1" >/proc/sys/net/ipv4/tcp_tw_reuse
    

    and/or

    echo "1" >/proc/sys/net/ipv4/tcp_tw_recycle
    

    These settings may make Linux re-use the TIME_WAIT sockets. Unfortunately I can't find any definitive documentation.

    0 讨论(0)
  • 2020-12-02 06:28

    If you are closing 500 connection per second you will run out of sockets. If you are connecting to the same locations (web servers) that use keepalive you can implement connection pools, so you don't close and reopen sockets.

    This will save cpu too.

    Use of tcp_tw_recycle and tcp_tw_reuse can result in packets coming in from the previous connecction, that is why there is a 1 minute wait for the packets to clear.

    0 讨论(0)
  • 2020-12-02 06:29

    Cannot assign requested address is the error string for the EADDRNOTAVAIL error.

    I suspect you are running out of source ports. There are 16,383 sockets in the dynamic range available for use as a source port (see RFC 2780). 150 clients * 100 connections = 15,000 ports - so you are probably hitting this limit.

    0 讨论(0)
  • 2020-12-02 06:38

    If you're running out of source ports but aren't actually maintaining that many open connections, set the SO_REUSEADDR socket option. This will enable you to reuse local ports that are still in the TIME_WAIT state.

    0 讨论(0)
提交回复
热议问题