首页 技术 正文
技术 2022年11月23日
0 收藏 686 点赞 5,076 浏览 5034 个字

 网络编程 — RPC实现原理 — 目录

  啦啦啦

V1——Netty入门应用

  Class : NIOServerBootStrap

package lime.pri.limeNio.netty.netty01.server;import java.net.InetSocketAddress;import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;public class NIOServerBootStrap { public static void main(String[] args) throws InterruptedException {
ServerBootstrap bootstrap = new ServerBootstrap();
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup worker = new NioEventLoopGroup();
bootstrap.group(boss, worker);
bootstrap.channel(NioServerSocketChannel.class); bootstrap.childHandler(new CustomServerChannelInitializer()); ChannelFuture channelFuture = bootstrap.bind(new InetSocketAddress(9999)).sync();
channelFuture.channel().closeFuture().sync();
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}

  Class : CustomServerChannelInitializer

package lime.pri.limeNio.netty.netty01.server;import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;public class CustomServerChannelInitializer extends ChannelInitializer<SocketChannel>{ @Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new CustomServerChannelHandlerAdapter());
}}

  Class : CustomServerChannelHandlerAdapter

package lime.pri.limeNio.netty.netty01.server;import java.util.Date;import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil;public class CustomServerChannelHandlerAdapter extends ChannelHandlerAdapter { /**
* 服务器和客户端会话异常
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// TODO Auto-generated method stub
super.exceptionCaught(ctx, cause);
} /**
* 当服务器与客户端联通时,通道被激活。
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
super.channelActive(ctx);
} /**
* 当服务器与客户端断开时,该方法被调用。
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
super.channelInactive(ctx);
} /**
* 通道读操作就绪,读取客户端消息
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buffer = (ByteBuf)msg;
String request = buffer.toString(CharsetUtil.UTF_8);
System.out.println("客户端请求数据:" + request); buffer.clear();
buffer.writeBytes(new Date().toString().getBytes());
ChannelFuture channelFuture = ctx.writeAndFlush(buffer);
channelFuture.addListener(ChannelFutureListener.CLOSE);
}}

  Class :

package lime.pri.limeNio.netty.netty01.client;import java.io.IOException;
import java.net.InetSocketAddress;import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;public class NIOBootStrap { public static void main(String[] args) throws IOException, InterruptedException {
Bootstrap bootstrap = new Bootstrap();
EventLoopGroup worker = new NioEventLoopGroup();
bootstrap.group(worker);
bootstrap.channel(NioSocketChannel.class);
bootstrap.handler(new CustomClientChannelInitializer());
ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress("127.0.0.1", 9999)).sync();
channelFuture.channel().closeFuture().sync();
worker.shutdownGracefully();
}
}

  Class : CustomClientChannelInitializer

package lime.pri.limeNio.netty.netty01.client;import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;public class CustomClientChannelInitializer extends ChannelInitializer<SocketChannel>{ @Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new CustomClientChannelHandlerAdapter());
}}

  Class : CustomClientChannelHandlerAdapter

package lime.pri.limeNio.netty.netty01.client;import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil;public class CustomClientChannelHandlerAdapter extends ChannelHandlerAdapter { /**
* 服务器和客户端会话异常
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// TODO Auto-generated method stub
super.exceptionCaught(ctx, cause);
} /**
* 当服务器与客户端联通时,通道被激活。
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ByteBuf buffer = Unpooled.buffer();
ctx.writeAndFlush(buffer.writeBytes("Query Date".getBytes()));
} /**
* 当服务器与客户端断开时,该方法被调用。
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
super.channelInactive(ctx);
} /**
* 通道多操作就绪,读取服务端消息
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("服务端响应数据:" + ((ByteBuf) msg).toString(CharsetUtil.UTF_8));
// 会话关闭操作由服务端启动,客户端不主动关闭会话。
}}

  Console : Server

客户端请求数据:Query Date

  Console : Client

服务端响应数据:Sat Jun 24 17:43:41 CST 2017

啦啦啦

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,257
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,695
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,535
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,306
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,944
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,104