使用Java NIO编写高性能的服务器 文件下载(2)
来源:互联网 发布:mysql in 很多 编辑:程序博客网 时间:2024/06/05 11:43
下面是客户端请求的代码,也比较简单,模拟100个用户同时下载文件。
package nio.file;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 文件下载客户端
* @author tenyears.cn
*/
public class NIOClient {
static int SIZE = 100;
static InetSocketAddress ip = new InetSocketAddress("localhost",12345);
static CharsetEncoder encoder = Charset.forName("GB2312").newEncoder();
static class Download implements Runnable {
protected int index;
public Download(int index) {
this.index = index;
}
public void run() {
try {
long start = System.currentTimeMillis();
SocketChannel client = SocketChannel.open();
client.configureBlocking(false);
Selector selector = Selector.open();
client.register(selector, SelectionKey.OP_CONNECT);
client.connect(ip);
ByteBuffer buffer = ByteBuffer.allocate(8 * 1024);
int total = 0;
FOR: for (;;) {
selector.select();
Iterator<SelectionKey> iter = selector.selectedKeys()
.iterator();
while (iter.hasNext()) {
SelectionKey key = iter.next();
iter.remove();
if (key.isConnectable()) {
SocketChannel channel = (SocketChannel) key
.channel();
if (channel.isConnectionPending())
channel.finishConnect();
channel.write(encoder.encode(CharBuffer
.wrap("Hello from " + index)));
channel.register(selector, SelectionKey.OP_READ);
} else if (key.isReadable()) {
SocketChannel channel = (SocketChannel) key
.channel();
int count = channel.read(buffer);
if (count > 0) {
total += count;
buffer.clear();
} else {
client.close();
break FOR;
}
}
}
}
double last = (System.currentTimeMillis() - start) * 1.0 / 1000;
System.out.println("Thread " + index + " downloaded " + total
+ "bytes in " + last + "s.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
ExecutorService exec = Executors.newFixedThreadPool(SIZE);
for (int index = 0; index < SIZE; index++) {
exec.execute(new Download(index));
}
exec.shutdown();
}
}
- 使用Java NIO编写高性能的服务器 文件下载(2)
- 使用Java NIO编写高性能的服务器 文件下载(转)
- 使用Java NIO编写高性能的服务器 文件下载(1)
- 使用Java NIO编写高性能的服务器 文件下载(转)
- 使用Java NIO编写高性能的服务器
- 使用Java NIO编写高性能的服务器
- 使用Java NIO编写高性能的服务器
- 使用Java NIO编写高性能的服务器
- 使用Java NIO编写高性能的服务器
- 使用Java NIO编写高性能的服务器
- 使用Java NIO编写高性能的服务器
- 用Java NIO编写高性能的服务器
- NIO单线程编写高性能、高并发服务器
- NIO单线程编写高性能、高并发服务器
- Java NIO.2 AIO 高性能服务器程序实例
- Java NIO.2 AIO 高性能服务器程序实例 收藏
- 如何编写高性能的网络服务器
- 如何编写高性能的网络服务器
- asp.net缓存技术与机制总结
- 基于NIO实现非阻塞Socket编程
- orcale数据库的导入导出
- 使用Java NIO编写高性能的服务器 文件下载(1)
- POI 1999 仓库管理员 Store−keeper
- 使用Java NIO编写高性能的服务器 文件下载(2)
- C#与SQL Server存储过程之一(创建):使用C#创建SQL Server的存储过程http://www.diybl.com/
- 软件工程常见问题和软工控制重要性
- JAVA上加密算法的实现用例
- 有向图强连通分量的Tarjan算法
- jsp验证码编写 Java2D
- Eclipse打包插件使用
- 关于 _declspec 的用法1------摘自MSDN
- 限制文本框的输入长度