java - How to detect end of Input Stream from Socket using BufferedInputStream? -


i'm trying read , input socket in java , proccess calling function. must read byte , not text because reading text give erros because of requests coming client. decided use class bufferedinputstream. problem end of request not being detected. guess because stream not ending being hanged or something...

according documentation, read() function returns byte read stream or -1 if stream has ended. so, code reads byte byte, joins in string, , then, when stream has ended, sends string proccessed (or @ least, supposed this). here code:

bufferedinputstream reader = new bufferedinputstream(socket.getinputstream()); int dt;    string cmd = "";  while( (dt = reader.read()) >= 0){   cmd += (char)dt == '\n' || (char)dt == '\r' ? ' ' : (char)dt; // add each byte read string    system.out.println(cmd); }  processainput(cmd); 

the problem function processainput never being called if program stuck in loop. more stranger is not stuck on loop because system.out.println stops being called after stream has ended. way, while being ran in thread (run() function), don't know if can mess added last info here. anyway, missing here? thanks.

edit: there no client side code because i'm using app postman google test it, sending http request program.

you conflating 'end of request' 'end of stream'.

'end of stream' on socket means peer has closed connection. no end of stream, no peer close. if plan write response down socket, therefore incorrect try read end of stream first.

you trying read single request, in case need in application protocol tell when have all: lines; length word prefix; self-describing protocol xml; stx/etx; type-length-value; ...

in case if protocol http need implement content-length header: recognise it, read end of headers, read many bytes stream, e.g. datainputstream.readfully().


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -