Server.java
public class Server { public static void main(String[] args) { //服务类 ServerBootstrap serverBootstrap = new ServerBootstrap(); //boss线程监听端口,worker线程负责数据读写 ExecutorService boss = Executors.newCachedThreadPool(); ExecutorService worker = Executors.newCachedThreadPool(); //设置niosocket工厂 serverBootstrap.setFactory(new NioServerSocketChannelFactory(boss, worker)); //设置管道的工厂 serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeLine = Channels.pipeline(); pipeLine.addLast("decoder", new StringDecoder()); pipeLine.addLast("encoder", new StringEncoder()); pipeLine.addLast("helloHandle", new HelloHandle()); return pipeLine; } }); serverBootstrap.bind(new InetSocketAddress(10102)); System.out.println("start!!!"); } }复制代码
HelloHandle.java
public class HelloHandle extends SimpleChannelHandler{ /** * 接收消息 */ @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { System.out.println("messageReceived"); String s = (String) e.getMessage(); System.out.println(s); //回写数据 ctx.getChannel().write("hi"); super.messageReceived(ctx, e); } /** * 捕获异常 */ @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { System.out.println("exceptionCaught"); super.exceptionCaught(ctx, e); } /** * 新连接 */ @Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { System.out.println("channelConnected"); super.channelConnected(ctx, e); } /** * 必须是链接已经建立,关闭通道的时候才会触发 */ @Override public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { System.out.println("channelDisconnected"); super.channelDisconnected(ctx, e); } /** * channel关闭的时候触发 */ @Override public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { System.out.println("channelClosed"); super.channelClosed(ctx, e); } }复制代码Netty3客户端代码
Client.java
public class Client { public static void main(String[] args) { //客户端类 ClientBootstrap strap = new ClientBootstrap(); ExecutorService boss = Executors.newCachedThreadPool(); ExecutorService worker =Executors.newCachedThreadPool(); //设置niosocket工厂 strap.setFactory(new NioClientSocketChannelFactory(boss,worker)); //设置管道的工厂 strap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeLine = Channels.pipeline(); pipeLine.addLast("decoder", new StringDecoder()); pipeLine.addLast("encoder", new StringEncoder()); pipeLine.addLast("hiHandle", new HiHandle()); return pipeLine; } }); //连接服务端 ChannelFuture connect = strap.connect(new InetSocketAddress("127.0.0.1",10102)); Channel channel = connect.getChannel(); System.out.println("client start"); Scanner scanner = new Scanner(System.in); while(true){ System.out.println("请输入"); channel.write(scanner.next()); } } }复制代码HiHandle.java
public class HiHandle extends SimpleChannelHandler { /** * 接收消息 */ @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { String s = (String) e.getMessage(); System.out.println(s); super.messageReceived(ctx, e); } /** * 捕获异常 */ @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { System.out.println("exceptionCaught"); super.exceptionCaught(ctx, e); } /** * 新连接 */ @Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { System.out.println("channelConnected"); super.channelConnected(ctx, e); } /** * 必须是链接已经建立,关闭通道的时候才会触发 */ @Override public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { System.out.println("channelDisconnected"); super.channelDisconnected(ctx, e); } /** * channel关闭的时候触发 */ @Override public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { System.out.println("channelClosed"); super.channelClosed(ctx, e); } }复制代码开始测试
1.启动服务端:
2.启动客户端:(并输入信息)
3.回看服务端
关注51码农网,寻找一起进步的同学。
51码农网 程序员社群学习打卡集聚地
关注51码农网官方微信公众号: