Search code examples
coldfusioncoldfusion-8

ColdFusion Socket Streaming, UTF-8 string not received properly on destination


I am using the easySocket udf and code below to send a 2 byte header length of the actual message via tcp/ip. However the UTF-8 character (i.e ( ) (8D hex) is received as ? (3F hex)). I have used that following code that I have also checked on stackoverflow. Is there a way to send byte equivalent of the length so that there will be no character encoding involve? Thanks.

    <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", "java.net.Socket" )>
        <cfset var streamOut = "">
        <cfset var output = "">
        <cfset var input = "">

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

        <cfif socket.isConnected()>
            <cfset streamOut = socket.getOutputStream()>

            <cfset output = createObject("java", "java.io.PrintWriter").init(streamOut)>
            <cfset streamInput = socket.getInputStream()>

            <cfset inputStreamReader= createObject( "java", "java.io.InputStreamReader").init(streamInput)>
            <cfset input = createObject( "java", "java.io.BufferedReader").init(InputStreamReader)>


            <cfset output.print(chr(1) & chr(141) & arguments.message)>
            <cfset output.flush()>

            <cfset result='Success: '& arguments.message >
            <cfset socket.close()>
        <cfelse>
            <cfset result = "Could not connected to host <strong>#arguments.host#</strong>, port <strong>#arguments.port#</strong>.">
        </cfif>

        <cfreturn result>
    </cffunction>

    <cfset txt_msg ='<COS MSG_TYPE_ID="0100" MCC="5999" POS_ENTRY_MODE="012" POS_COND="00" CARD_NUMBER="6417801234567890" ACQUIRER_ID="00000002" TERMINAL_ID="CAL100  " PROC_CODE="300000" STAN="000129" RETRIEVAL_REFERENCE="330145181129" DATE_LOCAL_TRAN="1111" TIME_LOCAL_TRAN="173654" TRANSM_DATE_TIME="1111173654" CARD_ACPT_ID_CODE="TESTDTA" CARD_ACPT_NAME_LOC="TSTESTSTE" />'>

    <cfoutput>#easySocket('172.16.0.23','9095',txt_msg)#</cfoutput>

Solution

  • According to the API, "... if you omit the encoding identifier, InputStreamReader and OutputStreamWriter rely on the default encoding." - which may not be "UTF-8". It is usually Cp1252 on windows machines. You can view the default by checking the system properties:

        <cfset system = createObject("java", "java.lang.System")>
        <cfset defaultEncoding = system.getProperty("file.encoding", "unknown")>
    

    .. or the encoding of your PrintWriter:

        <cfdump var="#output.getEncoding()#">
    

    Anyway, try specifying the encoding explicitly when you create the output stream. That should fix the issue.

     ... 
     <cfset streamOut = socket.getOutputStream()>
     <cfset writer = createObject("java", "java.io.OutputStreamWriter").init(streamOut, "UTF-8")>
     <cfset output = createObject("java", "java.io.PrintWriter").init(writer)>
     ...