NIO客户端(多线程)
来源:互联网 发布:数组与集合的区别 编辑:程序博客网 时间:2024/06/07 05:35
package com.clj;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.ClosedChannelException;import java.nio.channels.SelectionKey;import java.nio.channels.Selector;import java.nio.channels.SocketChannel;import java.util.Iterator;import java.util.Set;/** * NIO客户端线程类 * * @author chenlujun * @version [版本号, 2014年12月18日] * @see [相关类/方法] * @since [产品/模块版本] */public class NIOSocketClient extends Thread {private String IP="192.168.1.85";//服务器IPprivate int PORT=8989;//服务器端口private SocketChannel socketChannel;private Selector selector;/** 不带参数构造函数 */NIOSocketClient(){try {initClient();} catch (ClosedChannelException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/** 带参数构造函数 * @param url 服务器IP * @param port 客户端端口 */NIOSocketClient(String serverIp,int port){this.IP=serverIp;this.PORT=port;try {initClient();} catch (ClosedChannelException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public void run() {while (true) {try {// 写消息到服务器端writeMessage();int select = selector.select();if (select > 0) {Set<SelectionKey> keys = selector.selectedKeys();Iterator<SelectionKey> iter = keys.iterator();while (iter.hasNext()) {SelectionKey sk = iter.next();if (sk.isReadable()) {readMessage(sk);}iter.remove();}}} catch (Exception e) {e.printStackTrace();}}}//从服务端接收消息public void readMessage(SelectionKey sk) throws IOException,UnsupportedEncodingException {SocketChannel curSc = (SocketChannel) sk.channel();ByteBuffer buffer = ByteBuffer.allocate(8);while (curSc.read(buffer) > 0) {buffer.flip();System.out.println("Receive from server:"+ new String(buffer.array(), "UTF-8"));buffer.clear();}}//向服务端发送消息public void writeMessage() throws IOException {while(true){//每隔1秒向服务端发送一次数据try {Thread.sleep(1000);} catch (InterruptedException e1) {e1.printStackTrace();}try {String ss = "Server,how are you?";ByteBuffer buffer = ByteBuffer.wrap(ss.getBytes("UTF-8"));while (buffer.hasRemaining()) {System.out.println("buffer.hasRemaining() is true.");socketChannel.write(buffer);}} catch (IOException e) {if (socketChannel.isOpen()) {socketChannel.close();}e.printStackTrace();}}}//初始化客户端public void initClient() throws IOException, ClosedChannelException {InetSocketAddress addr = new InetSocketAddress(IP,PORT);socketChannel = SocketChannel.open();selector = Selector.open();socketChannel.configureBlocking(false);socketChannel.register(selector, SelectionKey.OP_READ);// 连接到serversocketChannel.connect(addr);while (!socketChannel.finishConnect()) {System.out.println("check finish connection");}}/** * 停止客户端 */public void stopServer() {try {if (selector != null && selector.isOpen()) {selector.close();}if (socketChannel != null && socketChannel.isOpen()) {socketChannel.close();}} catch (IOException e) {e.printStackTrace();}}}
package com.clj;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class Main {public static void main(String[] args) {//线程数int worker_num = 1000;//服务器IPString serverIp="192.168.1.85";//服务器端口int port=8989;//线程池ExecutorService threadPool = Executors.newFixedThreadPool(worker_num);for (int n = 0; n < worker_num; n++) {NIOSocketClient client=new NIOSocketClient(serverIp,port);threadPool.execute(client);}}}
0 0
- NIO客户端(多线程)
- socket nio 多线程接收客户端信息
- NIO客户端
- 客户端NIO实践分析
- 客户端NIO实践分析
- 客户端NIO实践分析
- 客户端NIO实践分析
- 【Java】NIO 客户端关闭
- 多线程NIO模仿Netty
- 基于事件的NIO多线程服务器(转)
- Java NIO客户端开发问题
- Android使用NIO开发客户端
- Android使用NIO开发客户端
- Java nio 客户端连接Server
- nio服务端以及普通客户端
- JAVA基于NIO客户端对客户端简单聊天DEMO(服务器转发消息)
- socket客户端服务器通信(多线程)
- 服务器与客户端(2):多线程
- ISO修改控件属性
- SparseArray详解
- 编程风格
- Android4.0添加按键以及key映射
- java用odbc连接orcale(配置odbc,连接代码)
- NIO客户端(多线程)
- JAVA编程思想---字符串操作
- eclipse搭建maven的web项目
- Servlet 工作原理解析
- IOS移动和方法小Demo
- 监控 Linux 性能
- OAuth1.0 简介及安全分析
- VC6.0 快捷键
- 配置JBoss数据源