I’ve read emails [1] [2] from a couple different people on the various email lists that CFMX can’t use sockets…. which is true in the sense that CFMX doesn’t have a <cfsocket> tag, but not true in the sense that you can’t easily use sockets in ColdFusion. I banged around this morning for a bit and came up with this example, which does an HTTP request of www.mindseye.com using a java.net.Socket object and then loops over the returned headers and prints them out to the screen:
<cfscript>
// get a socket object
socket = CreateObject(“java”, “java.net.Socket”);
// call the Socket constructor
socket.init(“www.mindseye.com”, 80);
// get a PrintWriter object
printWriter = CreateObject(“java”, “java.io.PrintWriter”);
// call the PrintWriter constructor
printWriter.init(socket.getOutputStream(), true);
// get an InputStreamReader object
inputsr = CreateObject(“java”, “java.io.InputStreamReader”);
// call the InputStreamReader constructor
inputsr.init(socket.getInputStream());
// get a BufferedReader object
input = CreateObject(“java”, “java.io.BufferedReader”);
// call the BufferedReader constructor
input.init(inputsr);
// request the homepage
printWriter.println(‘GET /index.cfm’);
// loop over result by reading line by line
run = true;
while (run) {
// read the result line by line
resultLine = input.readLine();
if (len(resultLine) GT 0) {
WriteOutput(“Header: ” & resultLine & “<br />”);
} else {
run = false;
}
}
</cfscript>
You could easily modify this to write xml to a server socket:
printWriter.println(‘<order orderType=”quote” customerType=”RETAIL” orderNumber=”1234″ orderDate=”2003-03-21″>’);
printWriter.println(‘<shipment ID=”1″ city=”Boston” state=”MA” zip=”02210″>’);
printWriter.println(‘<line_item ID=”1″ SKU=”18JK23″ category=”GSHOE” quantity=”2″ total=”39.90″ />’);
printWriter.println(‘</shipment>’);
printWriter.println(‘</order>’);
or do a variety of other useful things like send SMTP email, do DNS lookups, or send SOAP packets.
Very interesting article!!!,
but unfortunately i am not too familiar with Java. So my question is: how may i get the actual content of the page same way you got here the headers?
Other question is: is there anywhere any set of custom tags to perform different actions using sockets? or a good list of examples just like this one? Is not a secret that many times CFHTTP sux, so I am sure it will help many cf developers to get deeper into this field and to use this excelent opportunity.
Thank you in advance,
Ed.
thx for the good info here.
i am pretty new to java too but love the newfound functionality built into CFMX due to java.
i tried your example and it worked great. however, i want to connect to a java stream on an authenticated site such as:
58.36.55.25:80/page.cfm?username=ieru99&pwd=kafj99
when i add a url like this, i get:
Object Instantiation Exception.
An exception occurred when instantiating a java object.
any help would be greatly appreciated.
I tried the code u posted to create socket….Mine does not work….its throwing the foll. error: