– Common Function Library Project

easySocket(host, port, message)

Last updated August 27, 2009

Version: 1 | Requires: ColdFusion MX | Library: NetLib

Rated 18 time(s). Average Rating: 4.8

This function allows ColdFusion developers to connect to remote hosts through the TCP/IP protocol and transmit messages. Very useful for implementing chat rooms, integrate with third party applications such as merchant carts etc.

Return Values:
Returns a string.


view plain print about
#easySocket('','9999','Hello from ColdFusion')#


Name Description Required
host Host to connect to. Yes
port Port for connection. Yes
message Message to be sent. Yes

Full UDF Source:

view plain print about
 Connect to sockets through your ColdFusion application.
 Mods by Raymond Camden
 @param host      Host to connect to. (Required)
 @param port      Port for connection. (Required)
 @param message      Message to be sent. (Required)
 @return Returns a string. 
 @author George Georgiou ( 
 @version 1, August 27, 2009 

<cffunction name="easySocket" access="private" returntype="any" hint="Uses Java Sockets to connect to a remote socket over TCP/IP" output="false">

    <cfargument name="host" type="string" required="yes" default="localhost" hint="Host to connect to and send the message">
    <cfargument name="port" type="numeric" required="Yes" default="8080" hint="Port to connect to and send the message">
    <cfargument name="message" type="string" required="yes" default="" hint="The message to transmit">

   <cfset var result = "">
   <cfset var socket = createObject"java""" )>
   <cfset var streamOut = "">
    <cfset var output = "">
    <cfset var input = "">

      <cfset socket.init(,arguments.port)>
      <cfcatch type="Object">
         <cfset result = "Could not connected to host <strong></strong>, port <strong>#arguments.port#</strong>.">

         <cfreturn result>

   <cfif socket.isConnected()>
       <cfset streamOut = socket.getOutputStream()>
       <cfset output = createObject("java""").init(streamOut)>
       <cfset streamInput = socket.getInputStream()>
       <cfset inputStreamReader= createObject"java""").init(streamInput)>
       <cfset input = createObject"java""").init(InputStreamReader)>
       <cfset output.println(arguments.message)>
       <cfset output.println()> 
       <cfset output.flush()>
       <cfset result=input.readLine()>
       <cfset socket.close()>
      <cfset result = "Could not connected to host <strong></strong>, port <strong>#arguments.port#</strong>.">
   <cfreturn result>
blog comments powered by Disqus


Latest Additions

Adam Cameron Adam Cameron added
6 day(s) ago

Ray Ford Ray Ford added
21 day(s) ago

Henry Ho Henry Ho added
a while ago

Rick Root Rick Root added
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 48 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 9 time(s)

Darwan Leonardo Sitepu splitNumber
Rated 5.0, 8 time(s)

Created by Raymond Camden / Design by Justin Johnson