📄 timeclientselect.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -