【发布时间】:2020-05-30 04:00:39
【问题描述】:
我正在尝试通过带有 ByteBuf 的 netty 发送一个字符串。 首先,我将字符串转换为这样的字节数组:
byteBuf.writeInt(this.serverName.length());
byteBuf.writeInt(this.ipAdress.length());
byteBuf.writeBytes(this.serverName.getBytes(StandardCharsets.UTF_8));
byteBuf.writeBytes(this.ipAdress.getBytes(StandardCharsets.UTF_8));
这很好用,但我不知道如何读取字节以将它们转换回字符串?
我尝试过类似的方法:
int snLen = byteBuf.readInt();
int ipLen = byteBuf.readInt();
byte[] bytes = new byte[byteBuf.readableBytes()];
System.out.println(byteBuf.readBytes(bytes).readByte());
this.ipAdress = "";
必须有一些东西来取回字节。您可以从字符串发送字节,但不能在最后取回字节?似乎有一种方法可以做到这一点,但我不知道如何做到这一点。
我希望你们中的任何人都可以帮助我。 提前致谢! :)
【问题讨论】:
-
@tima
io.netty.buffer.ByteBuf.class != java.nio.ByteBuffer.class -
@Ferrybig 我现在看到了..