nioServerHandle = new NIOServerHandle(NIO_PORT);
new Thread(nioServerHandle, “Server”).start();
}
}
NIO服务器处理请求类
package com.ruider.customerNIO;
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;
/**
- Created by mahede on 2018/11/20.
*/
public class NIOServerHandle implements Runnable{
private int port;
private ServerSocketChannel serverSocketChannel;
private Selector selector;
private volatile boolean start;
public NIOServerHandle(int port) {
this.port = port;
try {
selector = Selector.open();
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(port), 1024);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
start = true;
System.out.println(“NIOServer启动服务,端口:” + port);
}catch (Exception e) {
System.out.println(“封装NIO服务失败”);
System.out.println(e)
【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
;
}
}
@Override
public void run(){
while(start) {
try{
//没1秒唤醒一次,轮询一遍事件
selector.select(1000);
Set selectionKeys = selector.selectedKeys();
Iterator it = selectionKeys.iterator();
SelectionKey selectionKey = null;
while(it.hasNext()) {
selectionKey = it.next();
it.remove();
try{
handleInput(selectionKey);
}catch(Exception e){
System.out.println(“服务获取信息失败”);
System.out.println(e);
}
}
}
catch (Exception e) {
System.out.println(e);
}
}
if(selector != null)
try{
selector.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public void stop() { start = false;}
private void handleInput(SelectionKey key) throws Exception{
if(key.isValid()) {
if(key.isAcceptable()) {
ServerSocketChannel channel = (ServerSocketChannel) key.channel();
SocketChannel socketChannel = channel.accept();
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
}
if(key.isReadable()) {
SocketChannel socketChannel = (SocketChannel)key.channel();
//分配1M的buffer
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
int size = socketChannel.read(byteBuffer);
if(size > 0) {
//将缓冲区当前的limit设置为position=0,用于后续对缓冲区的读取操作
byteBuffer.flip();
byte[] bytes = new byte[byteBuffer.remaining()];
//复制到bytes中
byteBuffer.get(bytes);
String contents = new String(bytes, “UTF-8”);
System.out.println(“NIO服务收到消息:”+contents);
String expression = new String(bytes,“UTF-8”);
System.out.println(“服务器收到消息:” + expression);
doWrite(socketChannel, “收到信息,你好,我是NIO”);
}
else if(size < 0) {
key.cancel();
socketChannel.close();
}
}
}
}
private void doWrite(SocketChannel channel, String msg) {
try {
byte[] bytes = msg.getBytes();
ByteBuffer writebuffer = ByteBuffer.allocate(bytes.length);
writebuffer.put(bytes);
//postion,limit值的交换,用于输出buffer
writebuffer.flip();
channel.write(writebuffer);
}catch (Exception e) {
System.out.print("[error]: NIOserver发送信息失败");
e.printStackTrace();
}
}
}
NIO 客户端代码
提示
-
类Client用于启动NIO客户端,传输参数只有IP、PORT
-
类ClientHandle用于NIO客户端请求服务端以及处理服务端的数返回数据
NIO客户端启动类
package com.ruider.customerNIO;
/**
- Created by mahede on 2018/11/21.
*/
public class Client {
private static final String IP_ADDRESS = “localhost”;
private static final int PORT = 1234;
private static ClientHandle clientHandle;
public static void start(){ start(IP_ADDRESS, PORT);}
private synchronized static void start(String host, int port) {
if(clientHandle != null) {
clientHandle.stop();
}
clientHandle = new ClientHandle(IP_ADDRESS, PORT);
new Thread(clientHandle, “Client”).start();
}
//向服务器发送消息
public static boolean sendMsg(String msg) throws Exception{
if(msg.equals(“q”)) return false;
clientHandle.sendMsg(msg);
return true;
}
}
客户端发送请求和处理返回数据类
package com.ruider.customerNIO;
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;
import java.util.Set;
/**
- Created by mahede on 2018/11/21.
*/
public class ClientHandle implements Runnable {
private String ipAddress;
private int port;
private SocketChannel socketChannel;
private Selector selector;
private static boolean start;
public ClientHandle(String host, int port) {
try{
this.ipAddress = host;
this.port = port;
selector = Selector.open();
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
//socketChannel.connect(new InetSocketAddress(ipAddress, port));
//socketChannel.register(selector, SelectionKey.OP_CONNECT);
start = true;
System.out.println(“客户端启动”);
}catch (Exception e) {
System.out.println(“客户端启动失败”);
e.printStackTrace();
}
}
public static void stop() { start = false;}
public void sendMsg(String msg) throws Exception{
socketChannel.register(selector, SelectionKey.OP_READ);
doWrite(socketChannel, msg);
}
@Override
public void run() {
try{
doConnect();
}catch (Exception e){
e.printStackTrace();
System.exit(1);
}
while (start) {
try {
selector.select(1000);
Set selectionKeys = selector.selectedKeys();
Iterator it = selectionKeys.iterator();
SelectionKey key = null;
while (it.hasNext()) {
key = it.next();
it.remove();
try {
handleInput(key);
}
catch (Exception e) {
if(key != null){
key.cancel();
if(key.channel() != null){
key.channel().close();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
try{
if(selector != null) {