timeclientselect.java
来自「java nio 编程一个实例子.服务端程序」· Java 代码 · 共 64 行
JAVA
64 行
package com.ronsoft.books.nio.channels;import java.nio.ByteBuffer;import java.nio.channels.DatagramChannel;import java.nio.channels.Selector;import java.nio.channels.SelectionKey;import java.net.InetSocketAddress;public class TimeClientSelect extends TimeClient{ private long giveup; private Selector selector; public TimeClientSelect (String [] argv, long timeout) throws Exception { super (argv); this.selector = Selector.open(); this.channel.configureBlocking (false); this.channel.register (selector, SelectionKey.OP_READ); this.giveup = System.currentTimeMillis() + timeout; } protected InetSocketAddress receivePacket (DatagramChannel channel, ByteBuffer buffer) throws Exception { buffer.clear(); while (true) { InetSocketAddress sa; sa = (InetSocketAddress) channel.receive (buffer); if (sa != null) { return (sa); } long sleepTime = giveup - System.currentTimeMillis(); if (sleepTime <= 0) { return (null); }System.out.println ("Selecting for " + (sleepTime / 1000) + " seconds"); selector.select (sleepTime); } } public static void main (String [] argv) throws Exception { TimeClientSelect client = new TimeClientSelect (argv, 5000); client.sendRequests(); client.getReplies(); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?