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.