How to read the data from datastream

704 views Asked by At

Please help me out on how to read the stream of data in java. My requirement is to make the telnet connection to the router. This part is accomplished. From the router, Have to connect to the xxx remote machine using its ip address and port number through telnet. While making this connection, i am getting some response. But while reading, the program control stops at read() method of InputStream class. Here are the code snippet which i am using to read the stream of data.

        buff = new byte[4*1024];
        ret_read = 0;

        do
        {
           ret_read = in.read(buff); // Program control gets hanged here. Once all the data are read...
           if(ret_read > 0)
           {
               System.out.println(new String(buff,0,ret_read));

           }
        }while(ret_read > 0);
1

There are 1 answers

3
Tim B On

What is happening is the read is blocking and waiting for more data to be sent on the stream, it will continue to do that until the stream is closed or more data is sent.

You need to either use a non-blocking read, put a timeout on the read, or close the stream server side after it finishes sending the data.