java - SMTPClient set timout for an open port -
my problem is: need discovery if 1 ip , port running smtp service. this, i'm using smtpclient try open connection. i'm using code below.
private static boolean validatesmtp(string ip, int port, int timeout) { smtpclient smtp = new smtpclient(); try { smtp.setconnecttimeout(timeout); smtp.connect(ip, port); return true; } catch (socketexception e) { logaplication.warning("ops... wrong", e); } catch (ioexception e) { logaplication.warning("ops... wrong", e); } finally{ smtp = null; } return false; }
it's working fine , i've gotten expected results, timeout has been problem. e.g: if try ip: 127.0.0.1 , port 80 (iis open port) connect step takes long (much more defined in timeout) throw exception
java.net.socketexception: connection reset
how can set timeout case? or existis way simple test ?
after take @ grepcode, found method connect(string host, int port):
opens socket connected remote host @ specified port , originating specified local address , port. before returning, _connect action() called perform connection initialization actions.
as port opened service, socket opened, not causing timeout (by socket), exception thrown "connectaction()" needed set global timeout smtpclient, used socket connection , inside of "connectaction()" . , did solve problem:
smtp.setdefaulttimeout(timeout);
with this, i've expected results for, open ports throws exceptions , of course, connection smtp services.
Comments
Post a Comment