如何确保我的服务器读取客户端写入的内容?

发布于 2024-10-31 04:18:26 字数 704 浏览 1 评论 0原文

我正在从套接字客户端编写一个字节数组,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪冰清 2024-11-07 04:18:26

我不确定到底要打印什么,但我可以告诉您, 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.

昵称有卵用 2024-11-07 04:18:26

您将获得相同的字节,这只是您如何解释它们的问题。如果您想以 String 形式查看字节,请使用此选项:

System.out.println("msg rcvd: " + new String(msgType, "UTF-8"));

请注意,您正在处理的字节具有正确的编码(我在这里假定为 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:

System.out.println("msg rcvd: " + new String(msgType, "UTF-8"));

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 its writeUTF() on the server side and ObjectInputStream.readUTF() on the client side.

时光瘦了 2024-11-07 04:18:26

如果您在一侧使用 ObjectOutputStream,则必须在另一侧使用 ObjectInputStream

在您的情况下,一个简单的 OutputStream (可能是缓冲的)和 .write().read() 就可以了。

但对于打印,如果您想要格式化输出,请不要使用 byte[].toString(),而使用 Arrays.toString()


编辑:我只是看到您甚至没有在发送端编写数组。所以你实际上只是读取了 ObjectOutputStream 标头。


来自评论:

我正在处理服务器端,我被告知我将发送一个字节数组。如何
我接收并打印该字节数组吗?在这种情况下,字节数组是文本/字符串的字节

这听起来像是服务器发送的内容类似于以某种编码(如 ASCII、UTF-8 或 ISO-8859-1)编码的字符串。如果是这样,在接收端你可以使用这样的东西:

String encoding = "UTF-8";
BufferedReader in =
    new BufferedReader(new InputStreamReader(connection.getInputStream(),
                                             encoding));
String line;
while((line = in.readLine()) != null) {
    System.out.println(line);
}

当然,确保编码与发送端实际使用的编码相同。

相应的发送代码可能是这样的:

String encoding = "UTF-8";
Writer w =
    new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(),
                                              encoding));
w.write("Hello World!\n");
w.write("And another line.\n");
w.flush();

If you use an ObjectOutputStream on one side, you'll have to use an ObjectInputStream 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(), use Arrays.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:

I am handling server side and I am told that I would be send a byte array. How
do I receive and print that byte array ? byte array in this case is bytes of text/strings

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:

String encoding = "UTF-8";
BufferedReader in =
    new BufferedReader(new InputStreamReader(connection.getInputStream(),
                                             encoding));
String line;
while((line = in.readLine()) != null) {
    System.out.println(line);
}

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:

String encoding = "UTF-8";
Writer w =
    new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(),
                                              encoding));
w.write("Hello World!\n");
w.write("And another line.\n");
w.flush();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文