1.nio的模拟
/**
* @description TODO
* @author fangHao
* @date 2020年7月8日
*/
public class TomcatServer {
static ByteBuffer buffer=ByteBuffer.allocate(512);
static List<SocketChannel> cList=new ArrayList<SocketChannel>();
/**
* @description TODO
* @return void
* @date 2020年7月8日
*/
public static void main(String[] args) {
try {
ServerSocketChannel serverSocket=ServerSocketChannel.open();
SocketAddress socketAddress=new InetSocketAddress("127.0.0.1", 8080);
serverSocket.bind(socketAddress);
serverSocket.configureBlocking(false);
while (true) {
for(SocketChannel socketChannel:cList) {
int read = socketChannel.read(buffer);
if(read > 0) {
buffer.flip();
byte[] bs=new byte[read];
buffer.get(bs);
String contentString=new String(bs);
System.out.println(contentString);
buffer.flip();
}
}
SocketChannel accept = serverSocket.accept();
if(null != accept) {
accept.configureBlocking(false);
cList.add(accept);
}
}
} catch (Exception e) {
}
}
}
selector
/**
* @description nio
* @author fangHao
* @date 2020年7月8日
*/
public class TomcatServer {
static ByteBuffer buffer=ByteBuffer.allocate(512);
static List<SocketChannel> cList=new ArrayList<SocketChannel>();
static Selector selector=null;
/**
* @description 主函数
* @return void
* @date 2020年7月8日
*/
public static void main(String[] args) {
try {
ServerSocketChannel serverSocket=ServerSocketChannel.open();
SocketAddress socketAddress=new InetSocketAddress("127.0.0.1", 8080);
serverSocket.bind(socketAddress);
serverSocket.configureBlocking(false);
selector=Selector.open();
serverSocket.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
while(selector.select(0) > 0) {
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectedKeys.iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
iterator.remove();
if(key.isAcceptable()) {
acceptHandler(key);
}else if (key.isReadable()) {
readHandler(key);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void acceptHandler(SelectionKey key) {
ServerSocketChannel channel = (ServerSocketChannel) key.channel();
try {
SocketChannel accept = channel.accept();
accept.configureBlocking(false);
ByteBuffer buffer=ByteBuffer.allocate(1024);
accept.register(selector, SelectionKey.OP_READ, buffer);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readHandler(SelectionKey key) {
SocketChannel client = (SocketChannel) key.channel();
ByteBuffer buffer=(ByteBuffer) key.attachment();
buffer.clear();
while(true) {
try {
int read=client.read(buffer);
if(read > 0) {
buffer.flip();
while(buffer.hasRemaining()) {
client.write(buffer);
}
buffer.clear();
}else if (read == 0) {
break;
}else {
client.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
模拟Netty
/**
* @description TODO
* @author fangHao
* @date 2020年7月9日
*/
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;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class NettyDemo {
private ServerSocketChannel server=null;
private Selector selector1=null;
private Selector selector2=null;
private Selector selector3=null;
int port = 9090;
public void initServer() {
try {
server=ServerSocketChannel.open();
server.configureBlocking(false);
server.bind(new InetSocketAddress(port));
selector1=Selector.open();
selector2=Selector.open();
selector3=Selector.open();
server.register(selector1, SelectionKey.OP_ACCEPT);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
NettyDemo nettyDemo=new NettyDemo();
nettyDemo.initServer();
NioThread t1 = new NioThread(nettyDemo.selector1, 2);
NioThread t2 = new NioThread(nettyDemo.selector2);
NioThread t3 = new NioThread(nettyDemo.selector3);
t1.start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
t3.start();
System.out.println("服务器启动了。。。。。");
}
}
class NioThread extends Thread{
Selector selector=null;
static int selectors=0;
int id= 0;
boolean boss= false;
static LinkedBlockingQueue<SocketChannel>[] queue;
static AtomicInteger idx=new AtomicInteger();
NioThread(Selector sel , int n){
this.selector=sel;
this.selectors = n;
boss =true;
queue=new LinkedBlockingQueue[selectors];
for (int i = 0; i < n; i++) {
queue[i] =new LinkedBlockingQueue<SocketChannel>();
}
}
NioThread(Selector sel){
this.selector=sel;
id=idx.getAndIncrement() % selectors;
}
@Override
public void run() {
while(true) {
try {
while(selector.select(10) > 0) {
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectedKeys.iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
iterator.remove();
if(key.isAcceptable()) {
acceptHandler(key);
}else if (key.isReadable()) {
readHandler(key);
}
}
}
if(!boss && !queue[id].isEmpty()) {
ByteBuffer buffer=ByteBuffer.allocate(2929);
SocketChannel client = queue[id].take();
client.register(selector, SelectionKey.OP_READ,buffer);
System.out.println("新客户端"+client.socket().getPort()+"分配到"+id);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @description TODO
* @return void
* @date 2020年7月9日
*/
private void readHandler(SelectionKey key) {
SocketChannel client = (SocketChannel) key.channel();
ByteBuffer buffer=(ByteBuffer) key.attachment();
buffer.clear();
while(true) {
try {
int read=client.read(buffer);
if(read > 0) {
buffer.flip();
while(buffer.hasRemaining()) {
client.write(buffer);
}
buffer.clear();
}else if (read == 0) {
break;
}else {
client.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @description
* @return void
* @date 2020年7月9日
*/
private void acceptHandler(SelectionKey key) {
ServerSocketChannel channel = (ServerSocketChannel) key.channel();
try {
SocketChannel accept = channel.accept();
accept.configureBlocking(false);
int num=idx.getAndIncrement() % selectors;
queue[num].add(accept);
} catch (IOException e) {
e.printStackTrace();
}
}
}