个性化阅读
专注于IT技术分析

Netty入门示例

服务端

package com._656463.netty.ch01;

import io.netty.bootstrap.ServerBootstrap;

import io.netty.channel.ChannelFuture;

import io.netty.channel.ChannelInitializer;

import io.netty.channel.ChannelOption;

import io.netty.channel.EventLoopGroup;

import io.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.SocketChannel;

import io.netty.channel.socket.nio.NioServerSocketChannel;

public class HelloServer {

public static void main(String[] args) {

//创建Boss和work的NioEventLoopGroup,两个都算无限循环

EventLoopGroup bossGroup = new NioEventLoopGroup(1);

EventLoopGroup workGroup = new NioEventLoopGroup();

try {

//创建服务端启动对象,配置参数

ServerBootstrap bootstrap = new ServerBootstrap();

bootstrap

//设置线程组

.group(bossGroup, workGroup)

//使用NioServerSocketChannel作为服务端的通道实现

.channel(NioServerSocketChannel.class)

//设置线程队列的连接个数

.option(ChannelOption.SO_BACKLOG, 128)

//设置保存活动的连接状态

.childOption(ChannelOption.SO_KEEPALIVE, true)

//创建一个通道初始对象

.childHandler(new ChannelInitializer() {

//给pipeline设置处理器

@Override

protected void initChannel(SocketChannel ch) throws Exception {

ch.pipeline().addLast(new HelloServerHandler());

}

});

ChannelFuture future = bootstrap.bind(8080).sync();

future.channel().closeFuture().sync();

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

bossGroup.shutdownGracefully();

workGroup.shutdownGracefully();

}

}

}

服务端处理器

package com_656463.netty.ch01;

import io.netty.buffer.ByteBuf;

import io.netty.buffer.Unpooled;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.ChannelInboundHandlerAdapter;

import io.netty.util.CharsetUtil;

import j艾薇a.util.concurrent.TimeUnit;

/**

* 自定义Handler

*/

public class HelloServerHandler extends ChannelInboundHandlerAdapter {

@Override

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

System.out.println(“server channelRead..”);

ByteBuf buf = (ByteBuf) msg;

System.out.println(“客户端【” + ctx.channel().remoteAddress() + “】发送消息 :” + buf.toString(CharsetUtil.UTF_8));

}

@Override

public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {

ctx.writeAndFlush(Unpooled.copiedBuffer(“hello client”, CharsetUtil.UTF_8));

}

@Override

public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

cause.printStackTrace();

ctx.close();

}

}

客户端

package com_656463.netty.ch01;

import io.netty.bootstrap.Bootstrap;

import io.netty.channel.ChannelFuture;

import io.netty.channel.ChannelInitializer;

import io.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.SocketChannel;

import io.netty.channel.socket.nio.NioSocketChannel;

public class HelloClient {

public static void main(String[] args) {

NioEventLoopGroup eventExecutors = new NioEventLoopGroup();

Bootstrap bootstrap = new Bootstrap();

try {

bootstrap.group(eventExecutors)

.channel(NioSocketChannel.class)

.handler(new ChannelInitializer() {

@Override

protected void initChannel(SocketChannel ch) throws Exception {

ch.pipeline().addLast(new HelloClientHandler());

}

});

ChannelFuture channelFuture = bootstrap.connect(“127.0.0.1”, 8080).sync();

channelFuture.channel().closeFuture().sync();

} catch (Exception e) {

e.printStackTrace();

} finally {

eventExecutors.shutdownGracefully();

}

}

}

客户端处理类

package com_656463.netty.ch01;

import io.netty.buffer.ByteBuf;

import io.netty.buffer.Unpooled;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.ChannelInboundHandlerAdapter;

import io.netty.util.CharsetUtil;

/**

* 自定义Handler

*/

public class HelloClientHandler extends ChannelInboundHandlerAdapter {

@Override

public void channelActive(ChannelHandlerContext ctx) throws Exception {

ctx.writeAndFlush(Unpooled.copiedBuffer(“hello server”,CharsetUtil.UTF_8));

}

@Override

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

System.out.println(“server channelRead..”);

ByteBuf buf = (ByteBuf) msg;

System.out.println(“服务端【” + ctx.channel().remoteAddress() + “】回复消息 :” + buf.toString(CharsetUtil.UTF_8));

}

@Override

public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

cause.printStackTrace();

ctx.close();

}

}

赞(0)
未经允许不得转载:srcmini » Netty入门示例