2020-08-13 NIO方式实现异步接收多个客户端的连接

pom.xml 内容如下

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  4.0.0

  com.packt.samples

  namenode

  jar

  1.0.0

  parquet-writer

  http://maven.apache.org

 

   

      junit

      junit

      3.8.1

      test

   

 

 

   

      org.apache.maven.plugins

      maven-assembly-plugin

      2.4

     

       

          jar-with-dependencies

       

       

         

            true

            com.packt.samples.NameNode

         

       

     

     

       

          assemble-all

          package

         

            single

         

       

     

   

 

代码如下

cat src/main/java/com/packt/samples/Server.java

package com.packt.samples;

import java.nio.ByteBuffer;

import java.nio.channels.ServerSocketChannel;

import java.nio.channels.Selector;

import java.nio.channels.SelectionKey;

import java.nio.channels.SocketChannel;

import java.util.concurrent.LinkedBlockingQueue;

import java.util.concurrent.BlockingQueue;

import java.util.Iterator;

import java.net.SocketAddress;

import java.net.InetSocketAddress;

import java.io.IOException;

public class Server {

private Listener listener;

Server() throws IOException {

this.listener = new Listener();

}

public void start() {

listener.start();

}

public void join() throws InterruptedException {

listener.join();

}

private class Listener extends Thread {

private ServerSocketChannel acceptChannel = null;

private Selector selector = null;

private InetSocketAddress address;

private int currentReader;

private int port;

public Listener() throws IOException {

currentReader = 0;

address = new InetSocketAddress("0.0.0.0", 8020);

acceptChannel = ServerSocketChannel.open();

acceptChannel.configureBlocking(false);

port = acceptChannel.socket().getLocalPort();

selector= Selector.open();

acceptChannel.socket().bind(address, 100);

acceptChannel.register(selector, SelectionKey.OP_ACCEPT);

}

@Override

public void run() {

while (true) {

SelectionKey key = null;

try {

getSelector().select();

Iterator iter = getSelector().selectedKeys().iterator();

while (iter.hasNext()) {

try {

key = iter.next();

if (key.isValid()) {

if (key.isAcceptable()) {

ServerSocketChannel server = (ServerSocketChannel) key.channel();

System.out.println("accept a new channel " + server.accept().getRemoteAddress());

//SocketChannel channel;

//while ((channel = server.accept()) != null) {

// System.out.println("accept a new channel ");

//}

}

}

} catch (IOException e) {

}

key = null;

}

} catch (Exception e) {

}

}

}

synchronized Selector getSelector() {

return selector;

}

}

}

------------------------------

cat src/main/java/com/packt/samples/NameNode.java

package com.packt.samples;

/**

* Hello world!

*

*/

public class NameNode

{

public static void main( String[] args ) throws java.io.IOException,InterruptedException {

Server server = new Server();

server.start();

server.join();

}

}

-----------------------

编译

mvn clean package -DskipTests

-----------------------

执行

java -jar target/namenode-1.0.0-jar-with-dependencies.jar

----------------------

另外开3个窗口,执行以下命令

[root@localhost ~]# telnet 192.168.1.15 8020

Trying 192.168.1.15...

Connected to 192.168.1.15.

Escape character is '^]'.

程序打印如下内容

accept a new channel /192.168.1.15:45798

accept a new channel /192.168.1.15:45800

accept a new channel /192.168.1.15:45802

你可能感兴趣的:(2020-08-13 NIO方式实现异步接收多个客户端的连接)