简体   繁体   中英

Socket connection - Java adds bytes

I want to send a string from my Android device to my node.js server through a socket. The connection works already, but whenever I send the string, the server receives additional characters.

This is what I receive when I send the String "hans" to the node.js server:

Buffer ac ed
Buffer 00 05 77 04 68 61 6e 73

as utf8 string:

??
♣w♦hans

Here is the Java part which sends the string:

clientSocket = new Socket("xxx.xxx.xxx.xxx",9988);
ObjectOutputStream clientOut = new ObjectOutputStream(clientSocket.getOutputStream());              

String sendString = "hans";             
clientOut.write(sendString.getBytes());
clientOut.flush();

So why does this happen?

You are using an ObjectOutputStream which is used to send serialized Objects and must be decoded by an ObjectInputStream on the other end. If you are just sending bytes and reading bytes on the other end, then you should just be using an OutputStream and an InputStream .

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