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);}}

原创粉丝点击