write()之后的Java NIO SocketChannel read()

时间:2016-02-17 19:36:59

标签: java sockets networking nio socketchannel

我有一个客户端/服务器应用程序,其中两个用户互相写入,也可以相互发送大文件,我使用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,Socket和SocketChannel等替换ServerSocket并开始使用java NIO时,它是这样的:

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

我需要的是这个

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并找到一种方法如何单独发送数据或者将第一个字符串,第二个字符串和long值的字节一起发送,然后检查所有字节是否存在并以某种方式划分它们,谢谢任何帮助

0 个答案:

没有答案