NIO
发布日期:2021-05-06 22:37:55 浏览次数:23 分类:技术文章

本文共 4262 字,大约阅读时间需要 14 分钟。

服务端

import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.*;import java.util.Iterator;public class MyServer {
private int size = 1024; private ServerSocketChannel serverSocketChannel; private ByteBuffer byteBuffer; private Selector selector; private int remoteClientNum = 0; public MyServer(int port) throws IOException {
initChannel(port); } public void initChannel(int port) throws IOException {
serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); serverSocketChannel.bind(new InetSocketAddress(port)); selector =Selector.open(); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); byteBuffer = ByteBuffer.allocate(size); } private void listener() throws IOException {
while(true){
int select = selector.select(); if(select==0){
continue; } Iterator
iterator = selector.selectedKeys().iterator(); while(iterator.hasNext()){
SelectionKey key = iterator.next(); if(key.isAcceptable()){
ServerSocketChannel server = (ServerSocketChannel) key.channel(); SocketChannel channel = server.accept(); registerChannel(selector,channel,SelectionKey.OP_READ); remoteClientNum++; System.out.println("客户端连接数"+remoteClientNum); write(channel,"hello client".getBytes()); } if(key.isReadable()){
read(key); } iterator.remove(); } } } private void registerChannel(Selector selector,SocketChannel socketChannel,int opRead) throws IOException {
if(socketChannel == null){
return; } socketChannel.configureBlocking(false); socketChannel.register(selector,opRead); } private void write(SocketChannel socketChannel,byte[] writeDate) throws IOException {
byteBuffer.clear(); byteBuffer.put(writeDate); byteBuffer.flip(); socketChannel.write(byteBuffer); } private void read(SelectionKey key) throws IOException {
SocketChannel socketChannel = (SocketChannel)key.channel(); int count; byteBuffer.clear(); while ((count=socketChannel.read(byteBuffer))>0){
byteBuffer.flip(); while (byteBuffer.hasRemaining()){
System.out.print((char) byteBuffer.get()); } byteBuffer.clear(); } if(count < 0 ){
socketChannel.close(); } } public static void main(String[] args) throws IOException {
MyServer myServer = new MyServer(9999); myServer.listener(); }}

客户端

import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.SocketChannel;public class MyClient {
private int size = 1024; private ByteBuffer byteBuffer; private SocketChannel socketChannel; public void connectServer() throws IOException {
socketChannel = SocketChannel.open(); socketChannel.connect(new InetSocketAddress("127.0.0.1",9999)); socketChannel.configureBlocking(false); byteBuffer = ByteBuffer.allocate(size); receive(); } private void receive() throws IOException {
while(true){
byteBuffer.clear(); int count; while ((count=socketChannel.read(byteBuffer))>0){
byteBuffer.flip(); while (byteBuffer.hasRemaining()){
System.out.print((char)byteBuffer.get()); } send2Server("sayhi".getBytes()); byteBuffer.clear(); } } } private void send2Server(byte[] bytes) throws IOException {
byteBuffer.clear(); byteBuffer.put(bytes); byteBuffer.flip(); socketChannel.write(byteBuffer); } public static void main(String[] args) throws IOException {
new MyClient().connectServer(); }}
上一篇:feign调用上传文件接口(MultipartFile)
下一篇:seata和postgresql和springcloud整合

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年03月11日 05时19分28秒