如何确保我的服务器读取客户端写入的内容?
我正在从套接字客户端编写一个字节数组,如下所示:
byte[] arr = { 49, 49, 49, 49, 49};
InetAddress address = InetAddress.getByName(host);
connection = new Socket(address, port);
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
在接收端,在服务器上我有:
byte[] msgType = new byte[5];
in = new BufferedInputStream(connection.getInputStream());
int bytesRead = in.read(msgType, 0, 5);
System.out.println("msg rcvd: " + msgType.toString());
在输出中我得到奇怪的字符串:
server: waiting for connection
server: connection received from localhost
server: Connection Successful
bytes read: 5
msg rcvd: ��w
如何确保我获得与从客户端发送的字节相同的字节?
I am writing a byte array from a socket client as:
byte[] arr = { 49, 49, 49, 49, 49};
InetAddress address = InetAddress.getByName(host);
connection = new Socket(address, port);
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
At receiving end, on the server I have:
byte[] msgType = new byte[5];
in = new BufferedInputStream(connection.getInputStream());
int bytesRead = in.read(msgType, 0, 5);
System.out.println("msg rcvd: " + msgType.toString());
In the output I get weird string:
server: waiting for connection
server: connection received from localhost
server: Connection Successful
bytes read: 5
msg rcvd: ��w
How can make sure I get same bytes as I sent from my client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定到底要打印什么,但我可以告诉您,
msgType.toString()
不会打印字节数组的内容。这里是我找到的一个方法的链接,该方法将打印出来字节数组以更有意义的方式。
I'm not sure exactly what are trying to print out, but I can tell you that
msgType.toString()
will not print the contents of the byte array.Here is a link I found to a method which will print out the byte array in a more meaningful fashion.
您将获得相同的字节,这只是您如何解释它们的问题。如果您想以
String
形式查看字节,请使用此选项:请注意,您正在处理的字节具有正确的编码(我在这里假定为 UTF-8)。由于您已经是
ObjectOutputStream
,因此您可以在服务器端使用它的writeUTF()
,并在客户端使用ObjectInputStream.readUTF()
边。You're getting the same bytes, it's just a matter of how you interpret them. If you want to see the bytes as a
String
use this instead:Be careful that the bytes you're dealing have the right encoding though (I assumed UTF-8 here). Since you're already
ObjectOutputStream
though, you could just use itswriteUTF()
on the server side andObjectInputStream.readUTF()
on the client side.如果您在一侧使用
ObjectOutputStream
,则必须在另一侧使用ObjectInputStream
。在您的情况下,一个简单的 OutputStream (可能是缓冲的)和
.write()
和.read()
就可以了。但对于打印,如果您想要格式化输出,请不要使用
byte[].toString()
,而使用Arrays.toString()
。编辑:我只是看到您甚至没有在发送端编写数组。所以你实际上只是读取了 ObjectOutputStream 标头。
来自评论:
这听起来像是服务器发送的内容类似于以某种编码(如 ASCII、UTF-8 或 ISO-8859-1)编码的字符串。如果是这样,在接收端你可以使用这样的东西:
当然,确保编码与发送端实际使用的编码相同。
相应的发送代码可能是这样的:
If you use an
ObjectOutputStream
on one side, you'll have to use anObjectInputStream
at the other side.In your case, a simple OutputStream (maybe buffered) and
.write()
and.read()
will do.But for printing, don't use
byte[].toString()
, useArrays.toString()
if you want to have a formatted output.Edit: I just see you are not even writing your array on the sending side. So you are actually only reading the ObjectOutputStream header.
From the comment:
This sounds like the server sends something like simply the Strings encoded in some encoding, like ASCII, UTF-8 or ISO-8859-1. If so, on the receiving end you can use something like this:
Of course, make sure the encoding is the same encoding as actually used on the sending side.
The corresponding sending code could be something like this: