{"id":418,"date":"2003-06-06T12:05:19","date_gmt":"2003-06-06T16:05:19","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=418"},"modified":"2003-06-06T12:05:19","modified_gmt":"2003-06-06T16:05:19","slug":"using-sockets-in-cfmx","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/06\/06\/using-sockets-in-cfmx\/","title":{"rendered":"Using Sockets in CFMX"},"content":{"rendered":"<p>I&#8217;ve read emails [<a href=\"http:\/\/lists.evolt.org\/archive\/Week-of-Mon-20030526\/141815.html\">1<\/a>] [<a href=\"http:\/\/www.sys-con.com\/fusetalk\/messageview.cfm?catid=6&amp;threadid=31424&amp;highlight_key=y&amp;keyword1=socket\">2<\/a>] from a couple different people on the various email lists that CFMX can&#8217;t use sockets&#8230;. which is true in the sense that CFMX doesn&#8217;t have a &lt;cfsocket&gt; tag, but not true in the sense that you can&#8217;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 <a href=\"http:\/\/java.sun.com\/j2se\/1.4.1\/docs\/api\/java\/net\/Socket.html\">java.net.Socket<\/a> object and then loops over the returned headers and prints them out to the screen:<\/p>\n<p>&lt;cfscript&gt;<br \/>\n\/\/ get a socket object<br \/>\nsocket = CreateObject(&#8220;java&#8221;, &#8220;java.net.Socket&#8221;);<\/p>\n<p>\/\/ call the Socket constructor<br \/>\nsocket.init(&#8220;www.mindseye.com&#8221;, 80);<\/p>\n<p>\/\/ get a PrintWriter object<br \/>\nprintWriter = CreateObject(&#8220;java&#8221;, &#8220;java.io.PrintWriter&#8221;);<\/p>\n<p>\/\/ call the PrintWriter constructor<br \/>\nprintWriter.init(socket.getOutputStream(), true);<\/p>\n<p>\/\/ get an InputStreamReader object<br \/>\ninputsr = CreateObject(&#8220;java&#8221;, &#8220;java.io.InputStreamReader&#8221;);<\/p>\n<p>\/\/ call the InputStreamReader constructor<br \/>\ninputsr.init(socket.getInputStream());<\/p>\n<p>\/\/ get a BufferedReader object<br \/>\ninput = CreateObject(&#8220;java&#8221;, &#8220;java.io.BufferedReader&#8221;);<\/p>\n<p>\/\/ call the BufferedReader constructor<br \/>\ninput.init(inputsr);<\/p>\n<p>\/\/ request the homepage<br \/>\nprintWriter.println(&#8216;GET \/index.cfm&#8217;);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br \/>\n\/\/ loop over result by reading line by line<br \/>\nrun = true;<br \/>\nwhile (run) {<br \/>\n&nbsp;&nbsp;&nbsp;\/\/ read the result line by line<br \/>\n&nbsp;&nbsp;&nbsp;resultLine = input.readLine();<br \/>\n&nbsp;&nbsp;&nbsp;if (len(resultLine) GT 0) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WriteOutput(&#8220;Header: &#8221; &amp; resultLine &amp; &#8220;&lt;br \/&gt;&#8221;);<br \/>\n&nbsp;&nbsp;&nbsp;} else {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run = false;<br \/>\n&nbsp;&nbsp;&nbsp;}<br \/>\n}&nbsp;&nbsp;&nbsp;<br \/>\n&lt;\/cfscript&gt;<\/p>\n<p>You could easily modify this to write xml to a server socket:<\/p>\n<p>printWriter.println(&#8216;&lt;order orderType=&#8221;quote&#8221; customerType=&#8221;RETAIL&#8221; orderNumber=&#8221;1234&#8243; orderDate=&#8221;2003-03-21&#8243;&gt;&#8217;);<br \/>\nprintWriter.println(&#8216;&lt;shipment ID=&#8221;1&#8243; city=&#8221;Boston&#8221; state=&#8221;MA&#8221; zip=&#8221;02210&#8243;&gt;&#8217;);<br \/>\nprintWriter.println(&#8216;&lt;line_item ID=&#8221;1&#8243; SKU=&#8221;18JK23&#8243; category=&#8221;GSHOE&#8221; quantity=&#8221;2&#8243; total=&#8221;39.90&#8243; \/&gt;&#8217;);<br \/>\nprintWriter.println(&#8216;&lt;\/shipment&gt;&#8217;);<br \/>\nprintWriter.println(&#8216;&lt;\/order&gt;&#8217;);<\/p>\n<p>or do a variety of other useful things like send SMTP email, do DNS lookups, or send SOAP packets.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve read emails [1] [2] from a couple different people on the various email lists that CFMX can&#8217;t use sockets&#8230;. which is true in the sense that CFMX doesn&#8217;t have a &lt;cfsocket&gt; tag, but not true in the sense that you can&#8217;t easily use sockets in ColdFusion. I banged around this morning for a bit &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/06\/06\/using-sockets-in-cfmx\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using Sockets in CFMX<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/418"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=418"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/418\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}