ITCOOKBOOK.com

 Options

Current Time:
Mon Sep 06, 2010 9:01 pm
View unanswered posts

Log in

Username:
Password:
Log me on automatically each visit

Topic Information

Determine if a port is open on Remote Server

 
Post new topic   Reply to topic    ITCOOKBOOK.com Forum Index -> ColdFusion MX7
View previous topic :: View next topic  
Author Message
cindy
Member


Joined: 02 Dec 2006
Posts: 22

PostPosted: Sun Dec 03, 2006 10:50 pm    Post subject: Determine if a port is open on Remote Server Reply with quote

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?
Back to top
View user's profile Send private message
JP
Fresher


Joined: 02 Dec 2006
Posts: 19
Location: New York, NY

PostPosted: Sun Dec 03, 2006 10:51 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    ITCOOKBOOK.com Forum Index -> ColdFusion MX7 All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Help US Help YOU

 

 

Please complete our short survey and save $17.50 on ITCOOKBOOK.COM.

 

 

Take a look at SAPTrends.com, a new blog dedicated to discussion of SAP Training trends in SAP ERP.

 

 

C# Interview Questions

 

.NET Interview Questions

 

Java Interview Questions

 

Python Interview Questions

 

ColdFusion Interview Questions

 

Linux Interview Questions

 

Linux LPIC 1 Certification Questions

 

Perl Programming Interview Questions