I am trying to read from a Socket using Buffered开发者_开发问答Reader as follows
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while ((line = in.readLine()) != null)
{
}
I input a string, say "AUTH", I am getting the value of line
variable as ÿûÿû ÿûÿû'ÿýÿûÿýAUTH
Any solution to this problem?
That just means the extra data is in the socket for whatever reason. My guess is that you're using telnet to connect to the server, and that's the telnet protocol negotiation.
Java won't add extra data to what's genuinely there.
bufferedReader prepends input
No it doesn't. Something is writing extra input that you aren't expecting. In this case, Telnet. Telnet is a protocol that includes more than just lines of text.
BufferedReader.readLine()
is for input that consists of "lines" make sure that you are providing input correctly. Alternatively, you can define your own terminator and then read input character by character using BufferedReader.read()
(use a while loop if you don't know the length of input like while (in.read()!=-1)
or something like this).
精彩评论