android - TCP Socket on JAVA - Any byte >= 128 is received as 65533 -


i implementing server on android , using:

while (!thread.currentthread().isinterrupted()) {     try {         int r;         string response = "";         while ((r = input.read()) > 0) {         ...         }     ... } 

i have 2 issues. if client sends me byte of value 0, not received server.

the second issue is, if byte value 128 or more, keep receiving value of 65533 or binary value of 11111101.

anyone knows how solve these issues. beginner in networking on java.

i having same issue , i've found answer in this question:

you're dealing binary data should using raw stream - don't turn reader, meant reading characters.

you're receiving 65533 because that's integer used "unicode replacement character" used when value can't represented real unicode character. exact behaviour of current code depend on default character encoding on system - again isn't should rely on.

my failing code (simplified):

inputstream = socket.getinputstream(); bufferedreader input = new bufferedreader(new inputstreamreader(is));  int data = input.read(); if(data >=0){     system.out.println("obtained: "+data); } 

like you, printing: obtained: 65533 when number greater 127 sent in other side.

to solve this, call inputstream.read() method instead of creating reader:

inputstream = socket.getinputstream();  int data = is.read(); if(data >=0){     system.out.println("obtained: "+data); } 

note: question marked duplicate 1 referenced.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -