TCPNIOSocket通道
来源:互联网 发布:奚梦瑶天涯扒皮知乎 编辑:程序博客网 时间:2024/06/06 04:03
package com.lanou.day25;import java.io.IOException;import java.net.InetSocketAddress;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;public class TestNIOServer1 { private Selector selector;public static void main(String[] args) throws IOException { TestNIOServer1 testNIOServer1=new TestNIOServer1(); testNIOServer1.init(); testNIOServer1.listen();}public void listen() throws IOException {System.out.println("服务器已经启动,开始监听"); while(true) { selector.select(); Iterator iterator=selector.selectedKeys().iterator(); while(iterator.hasNext()) { SelectionKey key=(SelectionKey) iterator.next(); iterator.remove(); if(key.isAcceptable()) { ServerSocketChannel ssc=(ServerSocketChannel) key.channel(); SocketChannel sc=ssc.accept(); sc.configureBlocking(false); sc.write(ByteBuffer.wrap("服务器接收到请求".getBytes())); sc.register(selector, SelectionKey.OP_READ); }else if(key.isReadable()){SocketChannel sc=(SocketChannel) key.channel();ByteBuffer buffer=ByteBuffer.allocate(1024);sc.read(buffer);byte[] buf=buffer.array();String result=new String(buf);System.out.println("接收到的结果为:"+result);} } }}public void init() throws IOException {ServerSocketChannel ssc=ServerSocketChannel.open();ssc.configureBlocking(false);ssc.socket().bind(new InetSocketAddress(8887));//选择器进行初始化selector=Selector.open();ssc.register(selector,SelectionKey.OP_ACCEPT);}}
package com.lanou.day25;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.SelectionKey;import java.nio.channels.Selector;import java.nio.channels.SocketChannel;import java.util.Iterator;public class TestNIOClient1 { private Selector selector;public static void main(String[] args) throws IOException { TestNIOClient1 testNIOClient1=new TestNIOClient1(); testNIOClient1.init("10.80.13.214", 8887); testNIOClient1.listen();}public void listen() throws IOException {System.out.println("开始");while(true) {selector.select(); Iterator iterator=selector.selectedKeys().iterator(); while(iterator.hasNext()) { SelectionKey key=(SelectionKey) iterator.next(); iterator.remove(); if(key.isConnectable()) { System.out.println("连接状态"); SocketChannel sc=(SocketChannel) key.channel(); if(sc.isConnectionPending()) { sc.finishConnect(); } sc.configureBlocking(false); ByteBuffer buffer=ByteBuffer.allocate(1024); sc.write( buffer.wrap("lala".getBytes())); } }}}public void init(String ip,int port) throws IOException {SocketChannel sc=SocketChannel.open();sc.configureBlocking(false);selector=Selector.open();sc.connect(new InetSocketAddress(ip, port));sc.register(selector, SelectionKey.OP_CONNECT);}}
阅读全文
0 0
- TCPNIOSocket通道
- 通道
- 通道
- 通道
- 通道
- stm32DMA通道 ADC通道
- 通道和文件通道
- 建立通道
- Alpha通道
- 通道 Channels
- 阿尔法通道
- 图像通道
- dsa通道
- 图像通道
- 什么是通道
- socket通道
- DMA通道
- 什么是通道
- 【CUGBACM15级BC第23场 A】hdu 5146 Sequence
- Lua 模拟面向对象
- python下双星号的意义
- MapReduce 的格式输入----NLineInputFormat
- 外部过滤器,程序和命令--混杂命令
- TCPNIOSocket通道
- 【CUGBACM15级BC第23场 B】hdu 5147 Sequence II
- 文件的上传与下载
- python中reshape的负参数
- js中事件绑定,$("body").on("click")与$("").click()区别
- MapReduce 的格式输入----MultipleInputs多个输入
- show()和exec()的区别
- 操作系统:进程管理(概念)
- 笔试笔记(六)