Tcp Socket On Java - Any Byte >= 128 Is Received As 65533
Solution 1:
I was having the same issue and I've found the answer in this question:
You're dealing with binary data so you should be using the raw stream - don't turn it into a Reader, which is meant for reading characters.
You're receiving 65533 because that's the integer used for the "Unicode replacement character" used when a value can't be represented as a real Unicode character. The exact behaviour of your current code will depend on the default character encoding on your system - which again isn't something you should rely on.
My failing code was like (simplified):
InputStream is = socket.getInputStream();
BufferedReader input = new BufferedReader(new InputStreamReader(is));
int data = input.read();
if(data >=0){
System.out.println("Obtained: "+data);
}
Like you, it was printing: Obtained: 65533
when a number greater than 127 was sent in the other side.
To solve this, just call the InputStream.read()
method instead of creating any reader
:
InputStream is = socket.getInputStream();
int data = is.read();
if(data >=0){
System.out.println("Obtained: "+data);
}
Note: Probably this question could be marked as duplicate to the one previously referenced.
Solution 2:
If the client sends me a byte of value 0, it is not received by the server.
Yes it is, but you're ignoring it:
while ((r = input.read()) > 0) {
You probably meant:
while ((r = input.read()) > -1) {
The second issue is, if the byte value is 128 or more, I keep receiving a value of 65533 or a binary value of 11111101.
It's not clear what you mean by that, but bear in mind that bytes are signed in Java - the range for a byte is [-128, 127]
.
You should almost certainly not be reading a single byte at a time. Instead, it's usually more appropriate to have a byte array, and call
int bytesRead;
while ((bytesRead = input.read(buffer)) > 0)
Alternatively, if the stream is meant to be text, wrap the input stream in an InputStreamReader
. Don't try to do binary to text conversion yourself - it's fraught with easy-to-miss corner cases.
Solution 3:
If the client sends me a byte of value 0, it is not received by the server.
It is received by your server host, passed to your server application, and ignored by your application code, because your loop stops when you receive it. You should be looping on >= 0
, not > 0
.
The second issue is, if the byte value is 128 or more, I keep receiving a value of 65533 or a binary value of 11111101.
No you don't. You will get a negative integer for any byte value >= 128, but neither 65533 nor 0b11111101 is among them. That could only happen if you read into a short, or cast the result into a short, or printed it as a short,and even in that case it only happens for one specific byte value, not all of them between 128 and 255.
Solution 4:
read() returns number of bytes from the input stream. So, 0/zero is valid and good size especially for network connection. read() returns -1 when end of stream is reached. So, you code should be
while ((r = input.read()) != -1){}
and it also should have something within loop to prevent alive connection from endless reading. Some timeout - end of loop when you got zero for 5 minutes.
Post a Comment for "Tcp Socket On Java - Any Byte >= 128 Is Received As 65533"