Socket IP and Port test to see if a DB is there
<cfset serverStruct = structNew()/>
<!--- The server IP you want to check --->
<cfset serverStruct.serverIp = "127.0.0.1"/>
<!--- The port you want to check. In our case MSSQL --->
<cfset serverStruct.serverPort = "1433"/>
<!--- The response --->
<cfset serverStruct.portConnected = ""/>
<!--- Create the socket object --->
<cfset objSocket = CreateObject("java", "java.net.Socket") />
<!--- Now we try to connect to our port --->
<cftry>
<!--- call the init constructor and connect to the IP and Port --->
<cfset objSocket.init(serverStruct.serverIp, serverStruct.serverPort)/>
<!--- Did we connect? --->
<cfset serverStruct.portConnected = objSocket.isConnected()/>
<!--- Close connection --->
<cfset objSocket.close()/>
<cfcatch type="any">
<!--- The connection wasnt made --->
<cfset serverStruct.portConnected = "No"/>
</cfcatch>
</cftry>
<cfoutput>Is the server and port there : #serverStruct.portConnected#</cfoutput>
<cfdump var="#serverStruct#"/>
The only weird thing here, and as i've only be been up an hour i havent looked into more (wheres my coffe), is that if the socket cannot connect CFMX bombs out. Hence the try and catch block. I was more expecting the isConnected() constructro to come back with a negative? Odd but i'll look into later.<!--- The server IP you want to check --->
<cfset serverStruct.serverIp = "127.0.0.1"/>
<!--- The port you want to check. In our case MSSQL --->
<cfset serverStruct.serverPort = "1433"/>
<!--- The response --->
<cfset serverStruct.portConnected = ""/>
<!--- Create the socket object --->
<cfset objSocket = CreateObject("java", "java.net.Socket") />
<!--- Now we try to connect to our port --->
<cftry>
<!--- call the init constructor and connect to the IP and Port --->
<cfset objSocket.init(serverStruct.serverIp, serverStruct.serverPort)/>
<!--- Did we connect? --->
<cfset serverStruct.portConnected = objSocket.isConnected()/>
<!--- Close connection --->
<cfset objSocket.close()/>
<cfcatch type="any">
<!--- The connection wasnt made --->
<cfset serverStruct.portConnected = "No"/>
</cfcatch>
</cftry>
<cfoutput>Is the server and port there : #serverStruct.portConnected#</cfoutput>
<cfdump var="#serverStruct#"/>