selector socketChannel
来源:互联网 发布:b2b自动发布软件 编辑:程序博客网 时间:2024/05/16 15:34
篇文章对NIO进行了简介,对Channel和Buffer接口的使用进行了说明,并举了一个简单的例子来说明其使用方法。
本篇则重点说明selector,Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件。这样,一个单独的线程可以管理多个channel,从而管理多个网络连接。
与selector联系紧密的是ServerSocketChannel和SocketChannel,他们的使用与上篇文章描述的FileChannel的使用方法类似,然后与ServerSocket和Socket也有一些联系。
本篇首先简单的进selector进行说明,然后一个简单的示例程序,来演示即时通讯。
Selector
使用传统IO进行网络编程,如下图所示:
每一个到服务端的连接,都需要一个单独的线程(或者线程池)来处理其对应的socket,当连接数多的时候,对服务端的压力极大。并使用socket的getInputStream。Read方法来不断的轮训每个socket,效率可想而知。
而selector则可以在同一个线程中监听多个channel的状态,当某个channel有selector感兴趣的事情发现,selector则被激活。即不会主动去轮询。如下图所示:
Selector使用如下示意:
- public static void main(String[] args) throws IOException {
- Selector selector = Selector.open();
-
- ServerSocketChannel sc = ServerSocketChannel.open();
- sc.configureBlocking(false);
- sc.socket().bind(new InetSocketAddress(8081));
-
-
- sc.register(selector, SelectionKey.OP_ACCEPT|SelectionKey.OP_CONNECT|SelectionKey.OP_READ|SelectionKey.OP_WRITE);
-
- while(true){
- selector.select();
- Set<SelectionKey> selectedKeys = selector.selectedKeys();
- Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
- while(keyIterator.hasNext()) {
- SelectionKey key = keyIterator.next();
- if(key.isAcceptable()) {
-
- } else if (key.isConnectable()) {
-
- } else if (key.isReadable()) {
-
- } else if (key.isWritable()) {
-
- }
- keyIterator.remove();
- }
- }
-
- }
极简即时通讯
本例子是是一个极为简单的例子,很多地方都不完善,但是例子可以很好的说明selector的使用方法。
本例子包含服务端和客户端两个部分,其中服务端采用两个selector,用来建立连接和数据的读写。两个selector在两个线程中。
服务端
客户端
-
-
-
-
-
-
- public class Client {
-
-
-
-
- private String self;
-
-
-
-
- private String to;
-
-
- private Selector selector;
-
- private ByteBuffer writeBuffer = ByteBuffer.allocate(512);
-
- private SocketChannel channel;
-
- private Object lock = new Object();
-
-
- private volatile boolean isInit = false;
-
-
- public Client(String self, String to) {
- super();
- this.self = self;
- this.to = to;
- }
-
-
-
-
-
-
-
- public void initClient(String ip,int port) throws IOException {
-
- channel = SocketChannel.open();
-
- channel.configureBlocking(false);
-
- this.selector = Selector.open();
-
-
-
- channel.connect(new InetSocketAddress(ip,port));
-
- channel.register(selector, SelectionKey.OP_CONNECT);
- }
-
-
-
-
-
- @SuppressWarnings("unchecked")
- public void listen() throws IOException {
-
-
- while (true) {
- synchronized (lock) {
- }
- selector.select();
-
- Iterator<SelectionKey> ite = this.selector.selectedKeys().iterator();
- while (ite.hasNext()) {
- SelectionKey key = ite.next();
-
- ite.remove();
-
- if (key.isConnectable()) {
- SocketChannel channel = (SocketChannel) key
- .channel();
-
- if(channel.isConnectionPending()){
- channel.finishConnect();
-
- }
-
- channel.configureBlocking(false);
-
-
-
- channel.register(this.selector, SelectionKey.OP_READ);
- isInit = true;
-
-
- } else if (key.isReadable()) {
- read(key);
- }
-
- }
-
- }
- }
-
-
-
-
-
- public void read(SelectionKey key) throws IOException{
-
- SocketChannel data = (SocketChannel) key.channel();
- ByteBuffer buffer = ByteBuffer.allocate(512) ;
- try {
- data.read(buffer );
-
- } catch (IOException e) {
- e.printStackTrace();
- data.close();
- return;
- }
- buffer.flip();
-
- byte[] msgByte = new byte[buffer.limit()];
- buffer.get(msgByte);
-
- Message msg = Message.getMsg(new String(msgByte));
- System.out.println("---收到消息--"+msg+" 来自 "+msg.getFrom());
-
- }
-
-
- private void sendMsg(String content){
- writeBuffer.put(content.getBytes());
- writeBuffer.flip();
- try {
- while (writeBuffer.hasRemaining()) {
- channel.write(writeBuffer);
- }
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- writeBuffer.clear();
- }
-
-
-
-
-
- public void start() throws IOException {
- initClient("localhost",8081);
- new Thread("reading"){
- public void run() {
- try {
- listen();
- } catch (IOException e) {
- e.printStackTrace();
- }
- };
- }.start();
-
- int time3 = 0;
-
- while(!isInit&&time3<3){
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- time3 ++;
- }
-
- System.out.println("--------开始注册------");
- Message re = new Message("", self, "");
- sendMsg(re.toString());
- try {
- Thread.sleep(200);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("-----注册成功----");
-
- String content ="";
- System.out.println("---- 请输入要发送的消息,按回车发送,输入 123 退出----------");
-
- Scanner s = new Scanner(System.in);
-
- while (!content.equals("123")&&s.hasNext()) {
- content = s.next();
- Message msg = new Message(content, self, to);
- msg.setType("1");
- sendMsg(msg.toString());
- if (content.equals("123")) {
- break;
- }
- System.out.println("---发送成功---");
-
- }
-
- channel.close();
- }
-
-
- }
客户端测试
- public class TestClient1 {
-
- public static void main(String[] args) throws IOException {
- Client c1 =new Client("1", "2");
-
- c1.start();
- }
- }
- public class TestClient2 {
- public static void main(String[] args) throws IOException {
- Client c2 =new Client("2", "1");
-
- c2.start();
- }
- }
结束
本文的例子极为简单,但是都经过测试。在编码的过程中,遇到的问题主要有两点:
1. channel.register()方法阻塞
2. 使用线程池遇到问题。本文最后在服务端的读写线程中,没有使用线程池,原因注释说的比较明白,也说明了使用线程池的一种设想。