不用netty实现OIO和NIO连接通信
来源:互联网 发布:淘宝oem化妆品假货店 编辑:程序博客网 时间:2024/05/20 22:35
首先是 OIO server端的
package com.fk.testIO;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import java.nio.charset.Charset;/** * Created by fengkai on 11/04/17. */public class OIO { public void serve(int port) throws IOException{ //绑定端口 final ServerSocket socket = new ServerSocket(port); for(;;){ final Socket clientSocket = socket.accept(); System.out.println("A connetction from " +clientSocket); new Thread(new Runnable() { @Override public void run() { OutputStream out; InputStream in; try{ in = clientSocket.getInputStream(); byte[] bytes = new byte[1024]; int bytelen = 0; int bytein = 0; StringBuffer sb = new StringBuffer(); while ((bytelen = in.read(bytes)) != -1){ String str = new String(bytes, bytein, bytelen); sb.append(str); } System.out.println(sb.toString()); out = clientSocket.getOutputStream(); out.write("HelloWorld".getBytes(Charset.forName("UTF-8"))); out.flush(); clientSocket.close(); }catch (IOException e){ e.printStackTrace(); } } }).start(); } } public static void main(String[] args) { final int port = 8888; new Thread(new Runnable() { @Override public void run() { try { new OIO().serve(port); } catch (IOException e) { e.printStackTrace(); } } }).start(); }}
接下来是NIO 的server
package com.fk.testIO;import java.io.IOException;import java.net.InetSocketAddress;import java.net.ServerSocket;import java.nio.ByteBuffer;import java.nio.channels.SelectionKey;import java.nio.channels.Selector;import java.nio.channels.ServerSocketChannel;import java.nio.channels.SocketChannel;import java.util.Iterator;import java.util.Set;/** * Created by fengkai on 11/04/17. */public class NIO { public void service(int port) throws IOException{ // 打开selector 处理channel ServerSocketChannel serverChannel = ServerSocketChannel.open(); serverChannel.configureBlocking(false); ServerSocket socket = serverChannel.socket(); InetSocketAddress adress = new InetSocketAddress(port); socket.bind(adress); Selector selector = Selector.open(); //注册selector serverChannel.register(selector, SelectionKey.OP_ACCEPT); final ByteBuffer msg = ByteBuffer.wrap("hello".getBytes()); for(;;){ //等待新的处理事件 try { selector.select(); } catch (IOException e) { e.printStackTrace(); break; } //所有的selectionKey实例 Set<SelectionKey> readKeys = selector.selectedKeys(); Iterator<SelectionKey> iterator = readKeys.iterator(); while (iterator.hasNext()){ SelectionKey key = iterator.next(); iterator.remove();; //查看它是否是等待接受的新连接 try { if(key.isAcceptable()){ ServerSocketChannel server = (ServerSocketChannel) key.channel(); SocketChannel client = server.accept(); client.configureBlocking(false); client.register(selector, SelectionKey.OP_WRITE|SelectionKey.OP_READ, msg.duplicate()); System.out.println("a new Connetion" + client); } //准备好写 if(key.isWritable()){ SocketChannel client = (SocketChannel) key.channel(); ByteBuffer buffer = (ByteBuffer) key.attachment(); while (buffer.hasRemaining()){ if(client.write(buffer) == 0){ break; } } client.close(); } } catch (IOException e) { e.printStackTrace(); key.cancel(); try { key.channel().close(); }catch (IOException cex){ cex.printStackTrace(); } } } } }}
1 0
- 不用netty实现OIO和NIO连接通信
- OIO和NIO区别
- NIO和OIO
- OIO和NIO的区别
- Java NIO框架Netty教程(五)-Netty中OIO模型(对比NIO)
- Java NIO框架Netty教程(十四)-Netty中OIO模型(对比NIO)
- Java NIO框架Netty教程(五)-Netty中OIO模型(对比NIO)
- OIO与NIO区别
- OIO、NIO、AIO区别
- OIO与NIO区别
- Java NIO通信框架: Netty
- Netty实现客户端和服务端的通信
- nio实战之netty实现
- Netty实现网络通信
- Netty ByteBuf和Nio ByteBuffer
- 01 NIO网络通信框架 Netty server和client的api
- 转:OIO、NIO、AIO的区别是什么
- 1、OIO与NIO代码示例比较
- android View绘制相关问题总结
- 文章标题
- c/c++ static
- mysql数据类型
- 集合
- 不用netty实现OIO和NIO连接通信
- jQuery 同一级元素和子元素的各种操作
- Hash(散列函数)
- java POI操作Excel代码收藏 (包括标题垂直水平居中)
- 笔试面试算法经典--二叉树的子结构(Java)
- 浅谈MySQL中优化sql语句查询常用的30种方法
- oracle 数据库组成部分(数据库(各种文件)+实例(SGA PGA + 后台进程))知识
- 查看端口占用情况
- android SQLite3常用命令&语法