Java IO多路复用技术简介


/**
 * @author qifuguang
 * @date 15-2-4 下午2:07
 */
public class TimeServerMain {
    public static void main(String[] args) throws Exception {
        // 启动时间服务器
        new Thread(new SelectorTimeServer()).start();
    }
}




/**
 * @author qifuguang
 * @date 15-2-4 下午2:09
 */
public class TimeClientMain {
    public static void main(String[] args) throws Exception {
        // 创建100个客户端连接到服务器
        for (int i = 0; i < 100; i++) {
            new Thread(new SelectorTimeClient(i + 1)).start();
        }
    }
}


import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

/**
 * @author qifuguang
 * @date 15-2-4 下午1:21
 */
public class SelectorTimeServer implements Runnable {
    private static final String TIME_ORDER = "Query Time";
    private Selector selector;
    private ServerSocketChannel serverChannel;
    private volatile boolean stop = false;

    /**
     * 创建Selector, 创建ServerSocketChannel,并设置为非阻塞模式, 注册到selector.
     *
     * @throws Exception
     */
    public SelectorTimeServer() throws Exception {
        selector = Selector.open();
        serverChannel = ServerSocketChannel.open();
        serverChannel.socket().bind(new InetSocketAddress(8080));
        serverChannel.configureBlocking(false);
        serverChannel.register(selector, SelectionKey.OP_ACCEPT);
    }

    /**
     * 轮询监听selector.
     */
    @Override
    public void run() {
        try {
            System.out.println("时间服务器启动!");
            while (!stop) {
                selector.select(1000);
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                while (iterator.hasNext()) {
                    SelectionKey key = iterator.next();
                    iterator.remove();
                    handleKey(key);
                }
            }
            if (selector != null) {
                selector.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 处理每一种selector感兴趣的事件.
     *
     * @param key 轮询监听得到的SelectionKey.
     */
    private void handleKey(SelectionKey key) {
        try {
            if (key.isValid()) {  // 如果连接成功
                if (key.isAcceptable()) {  // 监听到有新客户端连接
                    SocketChannel accept = ((ServerSocketChannel) key.channel()).accept(); // 建立与客户端的连接
                    accept.configureBlocking(false);  // 设置该连接为非阻塞模式
                    accept.register(selector, SelectionKey.OP_READ); // 将该连接注册到selector
                    System.out.println("发现有新客户端连接...");
                }
                if (key.isReadable()) {    // 监听到有客户端发送请求
                    SocketChannel channel = (SocketChannel) key.channel();
                    // 读取客户端发来的请求
                    ByteBuffer buff = ByteBuffer.allocate(1024);
                    int size = channel.read(buff);
                    if (size > 0) {
                        byte[] b = new byte[size];
                        buff.flip();
                        buff.get(b);
                        String order = new String(b, "UTF-8");
                        System.out.println("收到客户端命令:" + order);
                        String content = "";
                        if (order.equalsIgnoreCase(TIME_ORDER)) {
                            content = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                        } else {
                            content = "命令错误";
                        }
                        // 根据客户端发来的请求做出相应的动作,并将处理结果返回给客户端
                        doWrite(channel, content);
                    } else if (size < 0) {
                        channel.close();
                        key.cancel();
                    } else {
                        ;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 向指定的SocketChannel发送指定的消息。
     *
     * @param sc      需要向哪一个SocketChannel发送消息
     * @param content 需要发送的消息
     * @throws Exception
     */
    private void doWrite(SocketChannel sc, String content) throws Exception {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put(content.getBytes("UTF-8"));
        buffer.flip();
        sc.write(buffer);
        if (!buffer.hasRemaining()) {
            System.out.println("下发消息给客户端:" + content);
        }
    }
}
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

/**
 * @author qifuguang
 * @date 15-2-4 下午1:21
 */
public class SelectorTimeClient implements Runnable {
    private static final String TIME_ORDER = "Query Time";
    private SocketChannel channel;
    private Selector selector;
    private volatile boolean stop = false;
    private Integer index;

    /**
     * 创建Selector, SocketChannel.
     *
     * @param index 客户端编号.
     * @throws Exception
     */
    public SelectorTimeClient(Integer index) throws Exception {
        selector = Selector.open();
        channel = SocketChannel.open();
        channel.configureBlocking(false);
        this.index = index;
    }

    /**
     * 轮询监听selector刚兴趣的事件.
     */
    @Override
    public void run() {
        try {
            System.out.println("第" + index + "个客户端启动!");
            // 先尝试异步连接服务器, 如果连接成功,则只需要把channel注册到selector的READ事件,
            // 读取服务器返回的结果. 如果不成功(客户端已经向服务器发送了sync包,但是服务器没有返回ack包, 物理链路还没建立成功)
            // 则把该channel注册到selector的CONNECT事件, 等待服务器返回的ack包.
            if (channel.connect(new InetSocketAddress(8080))) {
                channel.register(selector, SelectionKey.OP_READ);
                doWrite(channel, TIME_ORDER);
            } else {
                channel.register(selector, SelectionKey.OP_CONNECT);
            }
            while (!stop) {
                selector.select(1000);
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                while (iterator.hasNext()) {
                    SelectionKey key = iterator.next();
                    SocketChannel sc = (SocketChannel) key.channel();
                    iterator.remove();
                    if (key.isValid()) {
                        if (key.isReadable()) {  // 监听到可读事件, 读取服务器返回的处理结果.
                            ByteBuffer buff = ByteBuffer.allocate(1024);
                            int size = sc.read(buff);
                            if (size > 0) {
                                byte[] b = new byte[size];
                                buff.flip();
                                buff.get(b);
                                System.out.println("第" + index + "个客户端获取服务器返回时间:" + new String(b));
                                stop = true;
                            } else if (size < 0) {
                                sc.close();
                                key.cancel();
                            } else {
                                ;
                            }
                        }
                        if (key.isConnectable()) {  //监听到服务器返回了ack包, 准备完成连接的建立
                            if (sc.finishConnect()) {  // 调用此方法完成物理链路连接的建立
                                sc.register(selector, SelectionKey.OP_READ); // 建立连接之后注册监听READ事件
                                doWrite(sc, TIME_ORDER);
                            } else {
                                System.exit(1);  //否则,程序退出
                            }
                        }
                    }
                }
            }
            if (selector != null) {
                selector.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 向指定的channel发送指定的消息.
     *
     * @param channel 向哪一个channel发送消息
     * @param content 需要发送的消息
     * @throws Exception
     */
    private void doWrite(SocketChannel channel, String content) throws Exception {
        ByteBuffer buff = ByteBuffer.allocate(1024);
        buff.put(content.getBytes("UTF-8"));
        buff.flip();
        channel.write(buff);
        if (!buff.hasRemaining()) {
            System.out.println("第" + index + "个客户端成功发送请求到服务器:" + content);
        }
    }
}



04-01
### Java I/O 使用教程及常见问题解决 #### 一、Java I/O 基础概述 Java 提供了一套丰富的输入输出(I/O)API 来处理数据流操作。这些 API 主要分为两大类:传统的基于流的 I/O 和较新的非阻塞式 I/O(NIO)。传统 I/O 是一种同步阻塞模型,而 NIO 则利用了多路复用技术来实现高效的异步通信。 - 阻塞 I/O 模型意味着程序会等待直到完成整个 I/O 操作[^1]。 - 文件字节流用于执行基本的文件读取和写入功能,通过 `FileInputStream` 和 `FileOutputStream` 类可以轻松实现文件拷贝等功能[^2]。 #### 二、Java I/O 的核心组件 以下是 Java I/O 中的一些重要组成部分: ##### 1. 字节流与字符流 Java I/O 支持两种主要的数据传输方式: - **字节流**:适用于任何类型的原始数据传输,例如图片或视频文件。 - **字符流**:专门设计用来高效地处理文本数据。 示例代码展示如何使用字节流进行简单的文件复制过程如下所示: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopyExample { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("source.txt"); FileOutputStream fos = new FileOutputStream("destination.txt"); int content; while ((content = fis.read()) != -1) { fos.write(content); } fis.close(); fos.close(); } } ``` ##### 2. 缓冲区(Buffered Streams) 为了提高性能,通常建议在实际应用中采用缓冲机制。这可以通过封装基础流对象至相应的缓冲流实例来达成。 ##### 3. 转换流(Conversion Streams) 当需要转换不同编码格式之间的字符串时非常有用。比如 InputStreamReader 可以把字节流转化为字符流以便于进一步解析。 #### 三、非阻塞 IO(NIO)简介及其优势 随着互联网的发展以及并发请求量的增长,传统的 BIO 已经无法满足高吞吐量的需求场景下的效率要求。因此引入了更先进的 NIO 技术框架。其显著特点是支持单线程管理多个连接的能力,并且只有当某个通道准备好接受新消息时才会触发回调函数通知应用程序去获取最新到达的信息包[^3]。 #### 四、常见错误排查指南 针对开发者经常遇到的一些典型异常情况提供解决方案提示: - 如果发生 FileNotFoundException,则需确认目标路径是否存在有效资源可供访问; - 对于 EOFException ,可能是由于试图超出预期结束位置继续读取造成的;此时应调整逻辑判断条件防止越界行为的发生; - 当遭遇 SocketTimeoutException 或 ConnectException 等网络层面的问题时,请核查服务器端口开放状态以及防火墙设置等因素影响正常握手建立流程的可能性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值