Java BIO NIO AIO,java面试sql题和答案

本文介绍了如何使用Java NIO实现服务器和客户端的通信。包括NIOServerHandle类,负责启动服务器并处理请求,以及客户端的启动和消息发送处理。代码展示了服务器接受连接、读取数据并响应的过程,以及客户端的连接、发送消息和接收数据的逻辑。
摘要由CSDN通过智能技术生成

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 客户端代码

提示

  1. 类Client用于启动NIO客户端,传输参数只有IP、PORT

  2. 类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) {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值