简体   繁体   中英

Java Socket creation takes more time

I have given Socket soc = new Socket(host,port); Now when the host machine is up and in running state, the socket is getting created immediately.

But when the machine is turned off or rebooted then this line takes around 40 seconds to respond. I tried using soc.setSoTimeout(timeout); But this line is used after creation of Socket. Hence it doesn't help much.

Also this seems to be a bug in JAVA itself. http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=5092063

I have tried few workarounds given in this link like adding the machine port and host name in the etc/hosts file. But it doesn't work. Because of this delay due to DNS resolution while socket creation, the response time of my system gets severely affected.

Any help would be greatly appreciated.

thanks, Sr

Use the connect with timeout method

// create an unconnected socket
Socket soc = new Socket();
soc.setSoTimeout(SO_TIMEOUT); // if you like

// connect (with timeout)
soc.connect(new InetSocketAddress(host, port), CONNECT_TIMEOUT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM