一起学netty(5)netty入门程序案例
服务端
package com.weblog.netty.basic.server;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class NettyServer {
public static void main(String[] args) throws Exception{
//创建两个线程组bossGroup和workGroup,含有的子线程NioEventLoop的个数默认为cpu核心数的两倍。
//bossGroup只处理连接请求,workGroup处理客户端消息收发的业务。
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workGroup = new NioEventLoopGroup(8);
//服务端对象
ServerBootstrap serverBootstrap = new ServerBootstrap();
//绑定线程组
serverBootstrap.group(bossGroup,workGroup)
//使用NioServerSocketChannel作为服务器的通道实现
.channel(NioServerSocketChannel.class)
//初始化服务器的连接队列大小,服务端处理客户端连接请求是顺序处理的,所以同一时刻只能处理一个客户端连接
//多个客户端同时来的时候,服务端将不能同时处理的客户端连接请求放在等待队列中等待处理。
.option(ChannelOption.SO_BACKLOG,1024)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
//解码器,把消息转成字符串
pipeline.addLast(new StringDecoder());
//编码器,把消息转成字符串
pipeline.addLast(new StringEncoder());
//客户端消息处理类
pipeline.addLast(new NettyServerHandler());
}
});
//绑定端口
ChannelFuture channelFuture = serverBootstrap.bind(8077).sync();
channelFuture.channel().closeFuture().sync();
}
}
服务端消息处理类
package com.weblog.netty.basic.server;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
/**
* @author: Jiajiajia
* @Date: 2021/8/7
* @Description: netty服务器端处理类
*/
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
/**
* 当客户端连接服务器时,会触发改方法
* @param ctx
* @throws Exception
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("有客户端连接成功");
}
/**
* 当客户端有数据发送到服务器时会触发该方法
* @param ctx
* @param msg
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("读取客户端发送的数据:"+msg);
ctx.writeAndFlush("hi");
}
/**
* 客户端断开连接的时候会触发该方法
* @param ctx
* @throws Exception
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("客户端断开连接");
}
/**
* 客户端连接发生异常的时候会触发该方法
* @param ctx
* @param cause
* @throws Exception
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("客户端连接发生异常,断开连接");
ctx.close();
}
}
一般只用java写netty的服务器端程序,连接测试,收发消息测试可以用telnet命令测试。
客户端
package com.weblog.netty.basic.click;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class NettyClient {
public static void main(String[] args) throws Exception{
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new StringDecoder());
pipeline.addLast(new StringEncoder());
pipeline.addLast(new NettyClientHandler());
}
});
//服务器ip端口
ChannelFuture sync = bootstrap.connect("127.0.0.1", 8077).sync();
Channel channel = sync.channel();
//发送消息
channel.writeAndFlush("hello");
}
}
客户端处理类
package com.weblog.netty.basic.click;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("读取服务器信息:"+msg);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("连接断开");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("连接异常");
}
}
fixed
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。