博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nio Client
阅读量:4507 次
发布时间:2019-06-08

本文共 5608 字,大约阅读时间需要 18 分钟。

public class NIOClient {    static int SIZE = 2;    final static int bufferSize = 500 * 1024;    static InetSocketAddress ip = new InetSocketAddress("localhost", 12345);    static CharsetEncoder encoder = Charset.forName("GB2312").newEncoder();    static class Download implements Runnable {        protected int index;        String outfile = null;        public Download(int index) {            this.index = index;            this.outfile = "c:\\" + index + ".rmvb";        }        public void run() {            FileOutputStream fout = null;            // FileChannel fcout = null;            try {                fout = new FileOutputStream(outfile);                // fcout = fout.getChannel();            } catch (FileNotFoundException e1) {                // TODO Auto-generated catch block                e1.printStackTrace();            }            try {                long start = System.currentTimeMillis();                // 打开客户端socket管道                SocketChannel client = SocketChannel.open();                // 客户端的管道的通讯模式                client.configureBlocking(false);                // 选择器                Selector selector = Selector.open();                // 往客户端管道上注册感兴趣的连接事件                client.register(selector, SelectionKey.OP_CONNECT);                // 配置IP                client.connect(ip);                // 配置缓存大小                ByteBuffer buffer = ByteBuffer.allocate(bufferSize);                int total = 0;                FOR: for (;;) {                    // 阻塞,返回发生感兴趣事件的数量                    selector.select();                    // 相当于获得感兴趣事件的集合迭代                    Iterator
iter = selector.selectedKeys() .iterator(); while (iter.hasNext()) { SelectionKey key = iter.next(); System.out.println("-----Thread " + index + "------------------" + key.readyOps()); // 删除这个马上就要被处理的事件 iter.remove(); // 感兴趣的是可连接的事件 if (key.isConnectable()) { // 获得该事件中的管道对象 SocketChannel channel = (SocketChannel) key.channel(); // 如果该管道对象已经连接好了 if (channel.isConnectionPending()) channel.finishConnect(); // channel.write(encoder.encode(CharBuffer // .wrap("d://film//" + index + ".mp3"))); // 往管道中写一些块信息 channel.write(encoder.encode(CharBuffer .wrap("d://film//1-hadoop-1.1.2.tar.gz"))); // 之后为该客户端管道注册新的感兴趣的事件---读操作 channel.register(selector, SelectionKey.OP_READ); } else if (key.isReadable()) { // 由事件获得通讯管道 SocketChannel channel = (SocketChannel) key .channel(); // 从管道中读取数据放到缓存中 int count = channel.read(buffer); System.out.println("count:" + count); if (count > 0) { // 统计读取的字节数目 total += count; // 这样一来从posistion~limit这段缓存数据是有效,可利用的 // buffer.flip(); buffer.clear(); // 往输出文件中去写了 if (count < bufferSize) { byte[] overByte = new byte[count]; for (int index = 0; index < count; index++) { overByte[index] = buffer.get(index); } fout.write(overByte); // System.out.println(":::" // + new String(buffer.array())); } else { fout.write(buffer.array()); // System.out.println(":::" // + new String(buffer.array())); } } else { // 关闭客户端通道 client.close(); // 退出大循环 break FOR; } // ? // buffer.clear(); } } } // 计算时间 double last = (System.currentTimeMillis() - start) * 1.0 / 1000; System.out.println("Thread " + index + " downloaded " + total / 1024 + "kbytes in " + last + "s."); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) throws IOException { long startTime = System.currentTimeMillis(); // 启用线程池 ExecutorService exec = Executors.newFixedThreadPool(SIZE); for (int index = 1; index <= SIZE; index++) { exec.execute(new Download(index)); } exec.shutdown(); long endTime = System.currentTimeMillis(); long timeLong = endTime - startTime; System.out.println("下载时间:" + timeLong); }

 

转载于:https://www.cnblogs.com/fuxinci/p/3201891.html

你可能感兴趣的文章
解决Android的ListView控件滚动时背景变黑
查看>>
laravel 多检索条件列表查询
查看>>
Java_基础—finally关键字的特点及作用
查看>>
SQLServer 日期函数大全
查看>>
激活webstorm11
查看>>
mysql 行转列 和 列转行
查看>>
[Leetcode]
查看>>
再谈vertical-align与line-height
查看>>
有关时延扩展的双语句子
查看>>
工作多年后积累的设计灵活,稳定,优秀WinForms应用程序的最佳实践 WinForms best practice...
查看>>
iOS开发——高级篇——iOS键盘的相关设置(UITextfield)
查看>>
JVMGC机制
查看>>
IAR for AVR 报array is too large错误 【已解决】
查看>>
老子《道德经》第六十二章
查看>>
Junit问题01 利用 @Autowired 注入失效问题
查看>>
20180711
查看>>
Js常见的创建对象
查看>>
IOS拖动
查看>>
httpclient的使用
查看>>
Kafka集群副本分配算法解析
查看>>