Java outgoing TCP connection failover based on multiple DNS results

我与影子孤独终老i 提交于 2019-12-04 11:35:39
bestsss

No! Creating a socket via new Socket(String, int) results in a resolving like that

addr = InetAddress.getByName(hostname);

which is a shortcut for

return InetAddress.getAllByName(host)[0];

The address resolution is performed in the Socket c-tor.

If you have to reconnect (failover) use the result returned by InetAddress.getAllByName(host), randomize (or use round-robin) and connect to the necessary addresses.

Edit: also if you are going to need to connect with some likely failure, you'd be better off using connect method of the Socket class with a timeout. Also make sure you close even failed sockets (and esp. channels) since they may leak a FD on *Nix.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!