java.net.socket cannot read

Hi,

I am accessing java objects through Cold Fusion (CFMX 6.1), and attempting to do a write+read using sockets.
I can do this fine if I do a "GET /index.cfm", but if I merely write a string to the socket, e.g. "00172AR", then the bufferedreader fails to read the returned string (i know the remote socket is returning  a string because I have tested it with a C++ socket program, and also done a packet sniff on the server).

The bufferedreader must be failing because the inputstream always returns 0 from its .available() method.

Any help most appreciated. 500 points (would give more but 500 is limit?)

here is my CF function, it uses CFscript, and nearly all the method calls to the java objects should be identical to Java.


<cffunction name="SocketIO" returntype="struct" access="public" output="true">
            <cfargument name="address" type="string" required="true">
            <cfargument name="port" type="numeric" required="true">
            <cfargument name="write_lines" type="string" required="true">
            <cfargument name="read_lines" type="numeric" required="false" default="1">
            <cfargument name="timeout" type="numeric" required="false" default=3000>
            <cfset var str = "empty">
            <cfset var socket       = createObject("java","java.net.Socket")>
            <cfset var instr       = createObject("java","java.io.InputStreamReader")>
            <cfset var input       = createObject("java","java.io.BufferedReader")>
            <cfset var output       = createObject("java","java.io.PrintStream")>
            <cfset var i = 1>
            <cfset var response = structnew()>
            <cfset response.data = arraynew(1)>
            <cfset response.connection = 0>
            <cfset response.output = 0>
            <cfset response.input = 0>
            <cfset response.status = 1>
            <cfset response.bytesavailable = 0>
            <cfscript>
                                                            
                  /** initialize our socket connection to the requested host **/
                  socket.init(arguments.address,arguments.port);
                  socket.setKeepAlive(true);
                  socket.setSoTimeout(arguments.timeout);
                  
                  if(socket.isConnected()){
                  
                        response.connection = 1;
                  
                        /** initialize our input/output streams  for reading and writing to the socket **/
                        
                        try{
                              
                              output.init(socket.getOutputStream(),true);
                              sock_in = socket.getInputStream();
                              instr.init(sock_in);
                              input.init(instr);
                                                      
                              if(not output.checkError()){
                                    output.println(arguments.write_lines);
                                    output.flush();
                                    
                                    if(not output.checkError()){
                                          response.output = 1;
                                    }
                              }
                              
                        }catch(Any err){
                              arrayappend(response.data,err.message);
                              response.output = 0;
                              response.status = 0;
                        }                        
                        
                        
                        /**if write was successful and there are bytes to read, then read**/
                                                                                                                                    
                        if(response.output and sock_in.available() gt 0){
                              
                              /** read all the requested lines from the input stream **/
                              response.bytesavailable = sock_in.available();                              
                              try{
                                                            
                                    for(i=1; i LTE read_lines; i=i+1){
                                          arrayappend(response.data,input.readLine());
                                    }
                                    
                              }catch(Any err){
                                    arrayappend(response.data,err.message);
                                    response.status = 0;
                              }
                              
                        }else{
                              response.status = 0;
                        }
                                    
                  }
                                    
                  /** close all our open streams/buffers/sockets **/
                  try{
                        input.close();
                        output.close();
                        inStr.close();
                        socket.close();      
                  }catch(Any err){
                        //don't do anything, if we got this far, we still want to return our response
                  }
                  
                  return response;
            </cfscript>
      </cffunction>

-H
LVL 4
hannemanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSoftware engineerCommented:
I quote:
Class InputStream

available()
The available method for class InputStream always returns 0.
This method should be overridden by subclasses.

0
hannemanAuthor Commented:
Ah, interesting zzynx, i will take that out, i had seen it used in some java code, so I thought i would try it.

If I take out all checking like that, the line:

input.readLine()

will return either "Read Timed Out" or "Connection Reset" if I increase the timeout to a large number.

0
hannemanAuthor Commented:
managed to solve it myself:

just do inputstream.read(), it will get back one byte at a time, which i can concatenate into a string.

funny that readline() didnt seem to work though!

-H

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

zzynxSoftware engineerCommented:
>> funny that readline() didnt seem to work though
A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
0
zzynxSoftware engineerCommented:
>> just do inputstream.read()
Bah ;°)
Would you believe that I thought that myself too?
But I didn't interprete your comment as "waiting for an answer" :°/
;°)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hannemanAuthor Commented:
Cheers anyway zzynx!
I guess the guys running the socket server arent putting in a "line ends here" flag of any kind!
-H
0
zzynxSoftware engineerCommented:
hanneman,
if you feel like I helped you a little bit, you can always ask for the points to be decremented (to say 50)
Just a proposal ;°)
0
hannemanAuthor Commented:
yeah okay! i have been beating my head against this brick wall for a week now, and without your first comment, i probably wouldnt have been able to solve it myself.... so:

ee_ai_construct (btw, very imaginative name for a robot....) please decrement points to 250 and give to zzynx.

am now going to the pub to have a beer and maybe dance a jig on a table....... :-)
0
zzynxSoftware engineerCommented:
Thanks for appreciating my efforts trying to help you.
Have a good beer/dance ;°)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.