4 Replies Latest reply on Sep 29, 2006 3:48 PM by bstenberg

    Open a Socket Connection

    bstenberg
      I have an RFID reader that has an internal memory bank that stores rfid scans. From speaking to the company in order to get the data off I need to create a socket connection to port 8080 and send RQL (which is similar to sql) and then it will send me back piped results.


      Can anyone assist me on how to open a connection to ip 192.168.1.139 port 8080 and send this

      SELECT read_count, protocol_id, antenna_id, id FROM tag_id WHERE protocol_id='GEN2' set time_out=8000

      and then output the results it returns.

      Thanks in advance!
        • 1. Re: Open a Socket Connection
          bstenberg Level 1
          I am using the below code found from another forum and it's working great... the issue is it's only returning one line.. I am however expecting more than that. Any suggestions?

          <!-- begin code sample -->
          <cfset serverStruct = structNew() />
          <!-- the variables -->
          <cfset serverStruct.serverIp = "192.168.1.139" />
          <cfset serverStruct.serverPort = "8080" />
          <cfset serverStruct.portConnected = "" />

          <!--- Create the socket object --->
          <cfset objSocket = CreateObject("java", "java.net.Socket") />

          <!--- Connect --->
          <cftry>
          <!--- Set up socket to IP:port of remote server --->
          <cfset objSocket.init(serverStruct.serverIp, serverStruct.serverPort)/>

          <cfset serverStruct.portConnected = objSocket.isConnected()>
          <!--- Did we connect? --->
          <cfif serverStruct.portConnected>
          <!-- create the string. Note the use of quotes to escape quotes in a string -->
          <cfset mySendData = "SELECT id FROM tag_id WHERE protocol_id='GEN2';">
          <!--- obtain the streams --->
          <cfset input = objSocket.getInputStream()>
          <cfset output = objSocket.getOutputStream()>
          <!--- set up buffered reader for the socket; it will read character data
          into a buffer until until enough of it can be processed efficiently. However,
          a buffered reader reads a reader object, hence we first have to convert the
          inputstream into a reader --->
          <cfobject class="java.io.BufferedReader" name="brObj" action="CREATE" type="JAVA">
          <cfobject class="java.io.InputStreamReader" name="isrObj" action="CREATE" type="JAVA">
          <cfset streamReader = isrObj.init(input)>
          <cfset dataBuffer = brObj.init(streamReader)>
          <!--- send the data to the remote server --->
          <cfobject class="java.io.PrintStream" name="pwObj" action="CREATE" type="JAVA">
          <cfset writer = pwObj.init(output)>
          <cfset sentData = writer.println(mySendData)>
          <!--- Get the response from the server --->
          <cfset response = dataBuffer.readLine()>

          <!--- Close the streams and the connection --->
          <cfset streamReader.close()>
          <cfset dataBuffer.close()>
          <cfset writer.close()>
          <cfset objSocket.close()>
          </cfif>

          <cfcatch type="any">
          something went wrong.
          </cfcatch>
          </cftry>

          <cfdump var="#response#">
          • 2. Re: Open a Socket Connection
            monkey_woo_too Level 1
            The answer is in your code:
            <cfset response = dataBuffer.readLine()>

            take a look at the java docs for BufferedReader: http://java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedReader.html

            because readLine returns null if you are passed the last line ( http://java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedReader.html#readLine()), you can do something like this:
            <cfset sLines = "">
            <cfset sLine = dataBuffer.readLine()>
            <cfloop condition="#isDefined('sLine')#">
            <cfset sLines = "#sLines##sLine#">
            <cfset sLine = dataBuffer.readLine()>
            </cfloop>
            <!--- now, sLines will contain all your lines of data--->

            here is a java example of the same thing... http://www.cafeaulait.org/slides/intljava/2001ny/javaio/57.html
            • 3. Re: Open a Socket Connection
              bstenberg Level 1
              Thanks for your response, ok i've change the code to use the following, but now it just hangs... i.e. it looks like it's working in the browser but then just times out.

              <cfsetting requesttimeout="1000">
              <cfflush interval="5">

              <!-- begin code sample -->
              <cfset serverStruct = structNew() />
              <!-- the variables -->
              <cfset serverStruct.serverIp = "192.168.1.139" />
              <cfset serverStruct.serverPort = "8080" />
              <cfset serverStruct.portConnected = "" />
              <cfset sLines = "">

              <!--- Create the socket object --->
              <cfset objSocket = CreateObject("java", "java.net.Socket") />

              <!--- Connect --->
              <cftry>
              <!--- Set up socket to IP:port of remote server --->
              <cfset objSocket.init(serverStruct.serverIp, serverStruct.serverPort)/>

              <cfset serverStruct.portConnected = objSocket.isConnected()>
              <!--- Did we connect? --->
              <cfif serverStruct.portConnected>
              <!-- create the string. Note the use of quotes to escape quotes in a string -->
              <cfset mySendData = "SELECT id FROM tag_id WHERE protocol_id='GEN2';">
              <!--- obtain the streams --->
              <cfset input = objSocket.getInputStream()>
              <cfset output = objSocket.getOutputStream()>
              <!--- set up buffered reader for the socket; it will read character data
              into a buffer until until enough of it can be processed efficiently. However,
              a buffered reader reads a reader object, hence we first have to convert the
              inputstream into a reader --->
              <cfobject class="java.io.BufferedReader" name="brObj" action="CREATE" type="JAVA">
              <cfobject class="java.io.InputStreamReader" name="isrObj" action="CREATE" type="JAVA">
              <cfset streamReader = isrObj.init(input)>
              <cfset dataBuffer = brObj.init(streamReader)>
              <!--- send the data to the remote server --->
              <cfobject class="java.io.PrintStream" name="pwObj" action="CREATE" type="JAVA">
              <cfset writer = pwObj.init(output)>
              <cfset sentData = writer.println(mySendData)>
              <!--- Get the response from the server --->
              <!---<cfset response = dataBuffer.readLine()>
              <cfset sLines = "">--->
              <cfset sLine = dataBuffer.readLine()>
              <cfloop condition="#isDefined('sLine')#">
              <cfset sLines = "#sLines##sLine#">
              <cfset sLine = dataBuffer.readLine()>
              </cfloop>


              <!--- Close the streams and the connection --->
              <cfset streamReader.close()>
              <cfset dataBuffer.close()>
              <cfset writer.close()>
              <cfset objSocket.close()>
              </cfif>

              <cfcatch type="any">
              something went wrong.
              </cfcatch>
              </cftry>

              <cfdump var="#sLine#">
              • 4. Re: Open a Socket Connection
                bstenberg Level 1
                maybe i am trying to do something coldfusion is not suppose to do.... but, I thought with the new event gateway's it was possible.

                Basically I need to open a tcp port to about 6 devices and keep that port opened while receiving messages from the RFID reader.

                Once a message is received it need to be inserted into a MS SQL database.

                That's it...

                Am i asking to much of CF?