简体   繁体   English

如何正确读取和写入 ByteBuffer?

[英]How to read from & write to ByteBuffer correctly?

I can't manage to get all data from byteBuffer.我无法从 byteBuffer 获取所有数据。 I have to methods as follows:我必须采用以下方法:

Client side:客户端:

public String sendMessage(String msg) {
        buffer = ByteBuffer.wrap(msg.getBytes());
        String response = null;
        try {
            client.write(buffer);
            buffer.clear();
            client.read(buffer);
            response = new String(buffer.array()).trim();
            System.out.println("response=" + response);
            buffer.clear();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }

Server side (There's another method which accepts clients and work with selection keys, I leave it out):服务器端(还有另一种接受客户端并使用选择键的方法,我将其省略):

private static void serverResponse(ByteBuffer buffer, SelectionKey key) throws IOException {
        SocketChannel client = (SocketChannel) key.channel();
        client.read(buffer);
        if (new String(buffer.array()).trim().equals("exit")) {
            client.close();
        }
        else {
            ByteBuffer responseBuffer = ByteBuffer.wrap("Example message".getBytes());
            client.write(responseBuffer);
            responseBuffer.clear();
        }
    }

When I call sendMessage() and get data from server within this method, I only receive a small piece of data (eg I get only "Examp" from original string "Example message").当我在此方法中调用 sendMessage() 并从服务器获取数据时,我只收到一小部分数据(例如,我只从原始字符串“示例消息”中获得“示例”)。 And only when I call sendMessage() again I receive the rest of the line (Also separately, I need to call sendMessage() a couple of times).只有当我再次调用 sendMessage() 时,我才会收到该行的 rest(另外,我需要调用 sendMessage() 几次)。 Once I reached the end of the line, it started looping and the next calling sendMessage() returns start of the line.一旦我到达行尾,它就开始循环,下一次调用 sendMessage() 返回行的开头。 How can I get the full data at once?如何一次获取完整数据?

I'm pretty sure the question has been answered, but I didn't find the solution myself.我很确定问题已得到解答,但我自己没有找到解决方案。 Please, help me by giving either the answer or a link to a related question请通过给出答案或相关问题的链接来帮助我

Note: I noticed that I only get as many characters from buffer as I sent to.注意:我注意到我从缓冲区中获得的字符数与发送到的字符数一样多。 So, I believe that the problem is in buffer capacity.所以,我认为问题在于缓冲能力。

Two issues:两个问题:

  1. After calling write you should call flush (in both cases).调用write后,您应该调用flush (在这两种情况下)。

  2. When you are reading the server's response (on the client side), you are using the same buffer which may not be big enough to hold the whole answer.当您读取服务器的响应(在客户端)时,您正在使用相同的缓冲区,该缓冲区可能不足以容纳整个答案。 The buffer size you are using is the size of the initial buffer you created in this line:您使用的缓冲区大小是您在此行中创建的初始缓冲区的大小:

     ByteBuffer.wrap(msg.getBytes());

Try to improve it with those suggestions, and see what happen.尝试用这些建议改进它,看看会发生什么。

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

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