cindy Member
Joined: 02 Dec 2006 Posts: 22
|
Posted: Sun Dec 03, 2006 10:50 pm Post subject: Determine if a port is open on Remote Server |
|
|
I am using CF 7 on Server 2003. I am trying to write a function to determine if a port is open on a remote server. I would like to do this within ColdFusion.
The problem I have with this method is that on fail, it takes 20 seconds to return. I have found methods using InetSocketAddress that allows you to set a timeout but that does not seem to be available within ColdFusion
Code:
<cfscript>
variables.strServer = 'www.blah.com';
variables.strPort = 3389;
try {
//instantiate a socket to the nessasary port
variables.connection = createObject("java","java.net.Socket").init(variables.strServer,variables.strPort);
//check to see if we are indeed connected
variables.strSuccess = connection.isConnected();
//close the connection, we are done with it
variables.connection = connection.close();
}
catch (any e) {
//if we timeout, no connection was made
variables.strSuccess = 'NO';
}
</cfscript>
Is there a better method to do this? |
|
JP Fresher
Joined: 02 Dec 2006 Posts: 19 Location: New York, NY
|
Posted: Sun Dec 03, 2006 10:51 pm Post subject: |
|
|
If you don't set the timeout either via setSoTimeout() or doing a connect() it defaults to 0, which is infinity. The setSOTimeout is used during reads so I guess you want the connect method. See if this code works any better.
Code:
<cfscript>
try {
host="your remote host goes here";
aPort=javacast("int",3389);
socket=createObject("java","java.net.Socket");
inetAddr=createObject("java","java.net.InetSocketAddress").init(host,aPort);
socket.connect(inetAddr,1);
socket.setSoTimeout(1);
socket.setSoLinger(false,0);
socket.setKeepAlive(false);
writeoutput("connected:=#socket.isConnected()#<br>
timeout:=#socket.getSoTimeout()#<br>
socket:=#socket.toString()#<br>
port=#socket.getPort()#<br>");
socket.close();
writeoutput("closed:=#socket.isClosed()#");
} // try
catch (Any e) {
socket.close();
}
</cfscript>
Your socket connection can fail for any number of reasons, not just timeout. If you specifically want to trap timeouts, you need to catch java.net.SocketTimeoutException. |
|