JAVA 网络编程(6) Netty TCP 示例

来源:互联网 发布:网络黑白txt下载 编辑:程序博客网 时间:2024/06/05 03:07

maven使用的netty版本如下:

<dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.4.Final</version></dependency>

示例代码:

import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelPipeline;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.codec.LengthFieldBasedFrameDecoder;import io.netty.handler.codec.LengthFieldPrepender;import io.netty.handler.codec.string.StringDecoder;import io.netty.handler.codec.string.StringEncoder;import io.netty.util.CharsetUtil;import util.LogCore;public class TcpServer {private static final String IP = "127.0.0.1";private static final int PORT = 9999;/** 用于分配处理业务线程的线程组个数 */protected static final int BIZGROUPSIZE = Runtime.getRuntime().availableProcessors() * 2; // 默认/** 业务出现线程大小 */protected static final int BIZTHREADSIZE = 4;/* * NioEventLoopGroup实际上就是个线程池, * NioEventLoopGroup在后台启动了n个NioEventLoop来处理Channel事件, * 每一个NioEventLoop负责处理m个Channel, * NioEventLoopGroup从NioEventLoop数组里挨个取出NioEventLoop来处理Channel */private static final EventLoopGroup bossGroup = new NioEventLoopGroup(BIZGROUPSIZE);private static final EventLoopGroup workerGroup = new NioEventLoopGroup(BIZTHREADSIZE);protected static void run() throws Exception {ServerBootstrap b = new ServerBootstrap();b.group(bossGroup, workerGroup);b.channel(NioServerSocketChannel.class);b.childHandler(new ChannelInitializer<SocketChannel>() {@Overridepublic void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));pipeline.addLast(new TcpServerHandler());}});b.bind(IP, PORT).sync();LogCore.BASE.info("TCP服务器已启动");}protected static void shutdown() {workerGroup.shutdownGracefully();bossGroup.shutdownGracefully();}public static void main(String[] args) throws Exception {LogCore.BASE.info("启动TCP服务器...");TcpServer.run();// TcpServer.shutdown();}}
import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import util.LogCore;public class TcpServerHandler extends SimpleChannelInboundHandler<Object> {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {LogCore.BASE.info("SERVER接收到消息:" + msg);ctx.channel().writeAndFlush("server accepted msg:" + msg);}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {LogCore.BASE.warn("exceptionCaught!", cause);ctx.close();}}

import io.netty.bootstrap.Bootstrap;import io.netty.channel.Channel;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelOption;import io.netty.channel.ChannelPipeline;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioSocketChannel;import io.netty.handler.codec.LengthFieldBasedFrameDecoder;import io.netty.handler.codec.LengthFieldPrepender;import io.netty.handler.codec.string.StringDecoder;import io.netty.handler.codec.string.StringEncoder;import io.netty.util.CharsetUtil;import util.LogCore;public class TcpClient {public static String HOST = "127.0.0.1";public static int PORT = 9999;public static Bootstrap bootstrap = getBootstrap();public static Channel channel = getChannel(HOST, PORT);/** * 初始化Bootstrap */public static final Bootstrap getBootstrap() {EventLoopGroup group = new NioEventLoopGroup();Bootstrap b = new Bootstrap();b.group(group).channel(NioSocketChannel.class);b.handler(new ChannelInitializer<Channel>() {@Overrideprotected void initChannel(Channel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));pipeline.addLast("handler", new TcpClientHandler());}});b.option(ChannelOption.SO_KEEPALIVE, true);return b;}public static final Channel getChannel(String host, int port) {Channel channel = null;try {channel = bootstrap.connect(host, port).sync().channel();} catch (Exception e) {LogCore.BASE.error("连接Server(IP{},PORT{})失败", host, port, e);return null;}return channel;}public static void sendMsg(String msg) throws Exception {if (channel != null) {channel.writeAndFlush(msg).sync();} else {LogCore.BASE.warn("消息发送失败,连接尚未建立!");}}public static void main(String[] args) throws Exception {try {long t0 = System.nanoTime();for (int i = 0; i < 100; i++) {TcpClient.sendMsg(i + "你好1");}long t1 = System.nanoTime();LogCore.BASE.info("time used:{}", t1 - t0);} catch (Exception e) {LogCore.BASE.error("main err:", e);}}}

import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import util.LogCore;public class TcpClientHandler extends SimpleChannelInboundHandler<Object> {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {LogCore.BASE.info("client接收到服务器返回的消息:" + msg);}}



0 0
原创粉丝点击