基于Nio的socket连接 随记

随便了解了下,也用搜到的代码理解了一下,稍微做了一些修改
package com.orz.gen;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.*;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: felix
 * Date: 2010-5-11
 * Time: 13:45:31
 */
public class GemClass {

    int port = 12345;
    int BUFFERSIZE = 1024;
    Selector selector = null;
    ServerSocketChannel serverChannel = null;
    HashMap<SocketChannel, ClientChInstance> clientChannelMap = null;//用来存放每一个客户连接对应的套接字和通道

    public GemClass(int port) {
        this.clientChannelMap = new HashMap<SocketChannel, ClientChInstance>();
        this.port = port;
    }

    public void initialize() throws IOException {
        //初始化,分别实例化一个选择器,一个服务器端可选择通道
        this.selector = Selector.open();
        this.serverChannel = ServerSocketChannel.open();
        this.serverChannel.configureBlocking(false);
        InetAddress localhost = InetAddress.getLocalHost();
        InetSocketAddress isa = new InetSocketAddress(localhost, this.port);
        this.serverChannel.socket().bind(isa);//将该套接字绑定到服务器某一可用端口
    }

    //结束时释放资源

    public void finalize() throws IOException {
        this.serverChannel.close();
        this.selector.close();
    }

    //将读入字节缓冲的信息解码

    public String decode(ByteBuffer byteBuffer) throws
            CharacterCodingException {
        Charset charset = Charset.forName("UTF-8");
        CharsetDecoder decoder = charset.newDecoder();
        CharBuffer charBuffer = decoder.decode(byteBuffer);
        return charBuffer.toString();
    }

    //监听端口,当通道准备好时进行相应操作
    public void portListening() throws IOException, InterruptedException {
        //服务器端通道注册OP_ACCEPT事件
        SelectionKey acceptKey = this.serverChannel.register(this.selector,
                SelectionKey.OP_ACCEPT);
        //当有已注册的事件发生时,select()返回值将大于0
        while (selector.select() > 0) {
            System.out.println("event happened");
            //取得所有已经准备好的所有选择键
            Set<SelectionKey> readyKeys = this.selector.selectedKeys();
            //使用迭代器对选择键进行轮询
            Iterator<SelectionKey> i = readyKeys.iterator();
            while (i.hasNext()) {
                SelectionKey key = i.next();
                i.remove();//删除当前将要处理的选择键
                if (key.isAcceptable()) {//如果是有客户端连接请求
                    System.out.println("more client connect in!");
                    ServerSocketChannel nextReady = (ServerSocketChannel) key.channel();
                    //获取客户端套接字
                    SocketChannel s = nextReady.accept();
                    //设置对应的通道为异步方式并注册感兴趣事件
                    s.configureBlocking(false);
                    //SelectionKey readWriteKey =s.register(this.selector,SelectionKey.OP_READ);
                    //将注册的事件与该套接字联系起来
                    //readWriteKey.attach(s);
                    s.register(selector,SelectionKey.OP_READ);
                    //将当前建立连接的客户端套接字及对应的通道存放在哈希表//clientChannelMap中
                    this.clientChannelMap.put(s, new ClientChInstance(s));
                } else if (key.isReadable()) {//如果是通道读准备好事件
                    System.out.println("Readable");
                    //取得选择键对应的通道和套接字
                    //SelectableChannel nextReady = key.channel();
                    SocketChannel channel = (SocketChannel) key.channel();
                    //处理该事件,处理方法已封装在类ClientChInstance中
                    this.readFromChannel(channel,clientChannelMap.get(channel));
                } else if (key.isWritable()) {//如果是通道写准备好事件
                    System.out.println("writeable");
                    //取得套接字后处理,方法同上
                    SocketChannel channel = (SocketChannel)key.channel();
                    this.writeToChannel(channel, "This is from server!");
                }
            }
        }
    }

    //对通道的写操作
    public void writeToChannel(SocketChannel channel, String message)
            throws IOException {
        ByteBuffer buf = ByteBuffer.wrap(message.getBytes());
        int nbytes = channel.write(buf);
        SelectableChannel ss;
    }

    //对通道的读操作
    public void readFromChannel(SocketChannel channel, ClientChInstance clientInstance)
            throws IOException, InterruptedException {
        ByteBuffer byteBuffer = ByteBuffer.allocate(BUFFERSIZE);
        int nbytes = channel.read(byteBuffer);
        byteBuffer.flip();
        String result = this.decode(byteBuffer);
        //当客户端发出”@exit”退出命令时,关闭其通道
        if (result.indexOf("@exit") >= 0) {
            channel.close();
        } else {
            clientInstance.append(result);
            //读入一行完毕,执行相应操作
            if (result.indexOf("\n") >= 0) {
                System.out.println("client input:  " + result);
                clientInstance.execute();
            }
        }
    }
    
    //该类封装了怎样对客户端的通道进行操作,具体实现可以通过重载execute()方法
    public class ClientChInstance {
        SocketChannel channel;
        StringBuffer buffer = new StringBuffer();

        public ClientChInstance(SocketChannel channel) {
            this.channel = channel;
        }

        public void execute() throws IOException {
            String message = "This is response after reading from channel!";
            writeToChannel(this.channel, message);
            buffer = new StringBuffer();
        }
        
        //当一行没有结束时,将当前字窜置于缓冲尾
        public void append(String values) {
            buffer.append(values);
        }
    }


    //主程序
    public static void main(String[] args) {
        GemClass nbServer = new GemClass(12345);
        try {
            nbServer.initialize();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
        try {
            nbServer.portListening();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}



比较底层的一些类有
ServerSocketChannel  //用于表示服务器,可以bind服务端地址和端口,关键在于使用accept()开启新的socket连接

SocketChannel; //代表一个socket连接的实例,在例子中使用了Map进行保存引用,进而复用,如保存交互记录等操作

Selector; //比较关键和比较难理解的一个地方,用于管理一组基于注册机制的连接对象,通过select()进行循环监听,在其下的连接对象发起新的事件时,响应并允许进行相应的自定义操作,使用selectedKeys表示所注册的子对象

SelectionKey; //配合Selector使用,用于表示一个或一组selector的类型,读/写/请求,比较抽象的包括了一个连接或者一个服务的类型(PS:比较纠结),因此也常需要进行强制向下转换.常用的方法有:channel(),attach(); 两个分别为关联的channel和所附带的对象,后者可能也可以用于保存一个数据.


这个例子忽略了线程安全带来的问题,可以直接使用telnet ip port进行测试,但如果涉及到服务端write操作,代码还需要调整,否则会有死循环产生!






你可能感兴趣的:(java,.net,socket,idea)