简体   繁体   中英

Unable to read data bytes from input stream in java tcp socket

In my Server code, I send different request to client and get back the response but only first read request is accessed, during accessing second read statement,it is unable to read Data bytes,my code is as followed.

private static boolean Rt_Request(int id,Object client)throws Exception  
{ 

int size=5; 

byte[] buf=new byte[size];

char[] cbuf=new char[32]; 

int byteRead; Socket s=(Socket)client;


BufferedReader in1= new BufferedReader(new InputStreamReader(s.getInputStream()));

PrintStream out=new PrintStream(s.getOutputStream());

try {
    buf[0]=0x02;            
    buf[1]=0x09;            
    buf[2]=0x01;            
    buf[3]=0x00;            
    buf[4]=0x03;
    Thread.sleep(5000);

    out.write(buf, 0, 5);      
} catch(Exception e) {        
     System.out.println("Error Occured...."+e);                             
}

byteRead=0;

while(byteRead!=1) {
        try {

            byteRead=in1.read(cbuf, 0, 1);// Have problem on this line,here I am unable to read data bytes.
            for(int i=0;i<byteRead;i++)
            {
            System.out.println(cbuf[i]);
            }
            if(byteRead==0)
            {
                System.out.println("Breaking.....");
                return false;
            }         
        }
        catch(Exception e) {
            System.out.println("Error Occured...."+e);
            return false;
        }

    }       
        return true;   
    } catch(Exception e) {
        System.out.println("System is not Connected..."+e);
        return false;
    }

almost tried every thing socket is not closed, read.available();,read.fully(); etc..unable to get the solution.I have written this function in the run method of TimerTask class. any help will be greatly appreciated

the javadocs says BufferedReader#read(char[], int, int) Returns: The number of characters read, or -1 if the end of the stream has been reached

since you do

byteRead=in1.read(cbuf, 0, 1);

in

while(byteRead!=1)

change it to

while(byteRead != -1)
byteRead=in1.read(cbuf, 0, 1);

该行仅读入一个值,并且由于您在进入for循环之前不会再次调用它,因此应该在标准输出中获得读取值的1 println。

The read method of the underlying InputStream will block (ie hang/wait) if no data is available.

This method blocks until input data is available, end of file is detected, or an exception is thrown.

I strongly suspect this is the case. You can check this by calling in1.ready() on the reader.

Flush the output buffer

out.flush();

after writing the bytes, or they may get buffered locally.

read() blocks until at least one byte is available. Maybe you haven't sent it, or flushed it properly, or maybe you are creating multiple BufferedReaders on the same socket.

NB bytesRead can never be zero after a successful read(cbuf, 0, 1).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM