write()后的Java NIO SocketChannel read()

我有一个客户端/服务器应用程序,其中两个用户互相写入,也可以将大型文件发送给对方,我使用ServerSocket和Socket类来执行此操作,除慢文件传输外,一切都很好,这是代码:

客户端:

public void sendFileRequest(Path filePath) {

    try {

        DataOutputStream output = new DataOutputStream(socket.getOutputStream());

        output.writeUTF("FILE_REQUEST");
        output.writeUTF(gui.getFilePath().toFile().getName());
        output.writeLong(gui.getFilePath().toFile().length());

    } catch (IOException e) {
        e.printStackTrace();
    }

}

服务器端:

DataInputStream codeInput = new DataInputStream(client1.getInputStream());
DataOutputStream codeOutput = new DataOutputStream(client2.getOutputStream());

String code = codeInput.readUTF();

                    switch (code) {

                    case "MESSAGE":

                        String message = codeInput.readUTF();
                        codeOutput.writeUTF(code);
                        codeOutput.writeUTF(message);

                        break;

                    case "FILE_REQUEST":

                        String fileName = codeInput.readUTF();
                        long fileSize = codeInput.readLong();

                        codeOutput.writeUTF(code);
                        codeOutput.writeUTF(fileName);
                        codeOutput.writeLong(fileSize);

                        break;

从IO切换到NIO(问题)

在“output.writeUTF(”FILE_REQUEST“);”之后“ 在客户端,客户端一直等到“codeInput.readUTF();” 在服务器上被调用,然后客户端继续。 问题是,当我用ServerSocketChannel替换ServerSocket时,使用SocketChannel的Socket等,并开始使用Java NIO,它是这样的:

客户端:写 - 客户端:写 - 客户端:写 - 服务器:读

我需要的是这个

client:write - server:read - client:write - server:read - client:write - server:read

在FILE_REQUEST的情况下,当我读取服务器端的代码时,出于某种原因,它只读取客户端的第一个写入操作,但当我尝试在服务器端读取第二次时,它读取第二个消息以及我发送的长整型值的字节

这是新的代码:

客户端:

//im using ByteBuffer
public void sendFileRequest(Path filePath) {

    try {

        writeString("FILE_REQUEST");
        //Here i need the code to wait until the string was read
        writeString(gui.getFilePath().toFile().getName());
        //Here i need the code to wait until the string was read
        writeLong(gui.getFilePath().toFile().length());

    } catch (IOException e) {
        e.printStackTrace();
    }

}

private void writeString(String s) throws IOException {

    buffer.put(s.getBytes());
    buffer.flip();
    channel.write(buffer);
    buffer.clear();

}

private void writeLong(long value) throws IOException {

    buffer.putLong(value);
    buffer.flip();
    channel.write(buffer);
    buffer.clear();

}

服务器端:

public void run() {

    String code = readString();

    switch (code) {

                    case "MESSAGE":

                        String message = readString();
                        writeString(code);
                        writeString(message);

                        break;

                    case "FILE_REQUEST":

                        String fileName = readString();
                        long fileSize = readLong();

                        writeString(code);
                        writeString(fileName);
                        writeLong(fileSize);

                        break;

}

private String readString() throws IOException {

            firstChannel.read(firstBuffer);
            firstBuffer.flip();
            byte[] bytes = new byte[firstBuffer.remaining()];
            firstBuffer.get(bytes);
            firstBuffer.clear();

            return new String(bytes);

        }

        private long readLong() throws IOException {

            firstChannel.read(firstBuffer);
            firstBuffer.flip();
            long value = firstBuffer.getLong();
            firstBuffer.clear();

            return value;

        }

我想使用Java NIO,并找到一种方法来单独发送数据或将第一个字符串,第二个字符串和长整型值的字节一起发送,然后检查是否所有字节都存在并以某种方式将它们分开,感谢任何帮助

链接地址: http://www.djcxy.com/p/34135.html

上一篇: Java NIO SocketChannel read() after write()

下一篇: Java NIO: When to properly switch between OP