简体   繁体   English

将字节写入Java套接字

[英]Writing bytes to java socket

I'm trying to write 0x87 in a Java socket I've created. 我正在尝试在创建的Java套接字中写入0x87。

tcpSocket = new Socket(server, port);
tcpWriteToServer = new PrintWriter(tcpSocket.getOutputStream(), true);
byte [] header = {0x8, 0x7};
tcpWriteToServer.print(header);

On the server side I get: 在服务器端,我得到:

Incoming Bytes: 0x5b 0x42 0x40 0x34 0x32 0x65 0x62 0x35 0x62 0x38 0x38 

I tried to break down the problem and just send one byte: 我试图解决问题,只发送一个字节:

tcpWriteToServer.print(header[0]);

For this I end up getting: 为此,我最终得到:

Incoming Bytes: 0x38

I'm not sure how that "3" in "0x38" gets read. 我不确定如何读取“ 0x38”中的“ 3”。 I read "0x37" when I try to send "0x7". 当我尝试发送“ 0x7”时,我读到“ 0x37”。

The server side is written in python and I have no issues sending the bytes and reading correctly in python. 服务器端是用python编写的,我在使用python发送字节并正确读取时没有任何问题。

Any idea what I might be missing? 知道我可能会缺少什么吗?

Any idea what I might be missing? 知道我可能会缺少什么吗?

You have to decide whether you want to write text or binary. 您必须决定要编写文本还是二进制文件。 Combining them is likely to be a good way to confuse yourself. 合并它们可能是使自己困惑的好方法。

I suggest you use binary in this case. 我建议您在这种情况下使用二进制文件。

tcpSocket = new Socket(server, port);
out = tcpSocket.getOutputStream();
out.write(0x8);
out.write(0x7);

You can also buffer the output like this. 您也可以像这样缓冲输出。

out = new BufferedOutputStream(tcpSocket.getOutputStream());
out.write(0x8);
out.write(0x7);
out.flush();

If you have an array of bytes you can write them in one go. 如果您有字节数组,则可以一次性写入。

byte[] header = { .... };
out.write(header);

I'm not sure how that "3" in "0x38" gets read. 我不确定如何读取“ 0x38”中的“ 3”。 I read "0x37" when I try to send "0x7". 当我尝试发送“ 0x7”时,我读到“ 0x37”。

There is no print(byte) but there is a print(int) so when you do 没有print(byte)但是有一个print(int)所以当您这样做时

print(8);

it is the same as 它与

print(Integer.toString(8));

or 要么

print("8");

or 要么

print('8')

and '8' = 0x38 in ASCII 并且ASCII中的'8'= 0x38

Incoming Bytes: 0x5b 0x42 0x40 0x34 0x32 0x65 0x62 0x35 0x62 0x38 0x38 传入字节:0x5b 0x42 0x40 0x34 0x32 0x65 0x62 0x35 0x62 0x38 0x38

If you print out character representation of each of this bytes 如果您打印出每个字节的字符表示形式

byte[] bytes = {0x5b, 0x42, 0x40, 0x34, 0x32, 0x65, 0x62, 0x35, 0x62, 0x38, 0x38};
for (byte b : bytes){
    System.out.print((char)b);
}

you will see that your code sends [B@42eb5b88 . 您会看到您的代码发送了[B@42eb5b88

It happens because since PrintWriter has no print(byte[]) method it uses print(Object obj) which just sends String representation of object obj . 发生这种情况是因为因为PrintWriter没有print(byte[])方法,所以它使用了print(Object obj) ,该方法只发送对象obj String表示形式。 Such string is created with String.valueOf(obj) which internally is using obj.toString() method, which for arrays returns typeName ( [b - one dimensional [ array of bytes b ), @ and hexHashCode . 此类字符串是使用String.valueOf(obj)创建的,该字符串内部使用obj.toString()方法,该方法对于数组返回typeName[b一维[字节数组b ), @hexHashCode

In other words you didn't send content of header array, but result of its toString() method. 换句话说,您不是发送header数组的内容,而是发送其toString()方法的结果。

If you want to send bytes it is easier to use Stream (Writers ware meant to handle text, not bytes) and their write(byte[]) method. 如果要发送字节,则使用Stream (用于处理文本而不是字节的Writers固件)及其write(byte[])方法更容易。 If you want to be also able to send text instead of bytes you can use PrintStream 如果您还希望发送文本而不是字节,则可以使用PrintStream

PrintStream tcpWriteToServer = new PrintStream(tcpSocket.getOutputStream(), true);    
tcpWriteToServer.write(header);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM