package com.example.demohystrix.process;

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;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.codec.string.StringEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.nio.charset.Charset; /**
* @author liusongwei
* @Title: NettyServer
* @ProjectName claimfront
* @Description: TODO
* @date 2019/3/2216:26
*/
public class NettyServer {
private static Logger log = LoggerFactory.getLogger(NettyServer.class); private final int port; public NettyServer(int port) {
this.port = port;
} public void start() throws Exception {
//用来接收进来的连接
EventLoopGroup bossGroup = new NioEventLoopGroup(); //用来处理已经被接收的连接
EventLoopGroup group = new NioEventLoopGroup();
try {
//启动 NIO 服务的辅助启动类
ServerBootstrap sb = new ServerBootstrap();
sb.option(ChannelOption.SO_BACKLOG, );//设置TCP缓冲区
// sb.option(ChannelOption.SO_SNDBUF,32*1024);//设置发送缓冲大小
// sb.option(ChannelOption.SO_RCVBUF,32*1024);//设置接收缓冲大小
// sb.option(ChannelOption.SO_KEEPALIVE,true);//保持连接
sb.group(group, bossGroup) // 绑定两个线程组
.channel(NioServerSocketChannel.class) // 指定NIO模式
.localAddress(this.port)// 绑定监听端口
// 绑定客户端连接时候触发操作 添加一个EchoServerHandler到子Channel的ChannelPipeline
//ChannelInitializer 当一个新的连接被接收时,一个新的子Channel将会被创建
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
log.info("客户端连接,连接的IP:{}",ch.localAddress().getHostName());
ch.pipeline().addLast(new StringEncoder(Charset.forName("GBK")));
//EchoServerHandler被标注为@Shareable,所以我们可以总是使用同样的实例
ch.pipeline().addLast(new NettyServerHandler()); // 客户端触发操作
ch.pipeline().addLast(new ByteArrayEncoder());
}
});
//异步绑定服务器,调用sync()方法阻塞等待直到绑定完成
ChannelFuture cf = sb.bind().sync(); //获取Channel的closeFuture,并且阻塞当前线程直到它完成
cf.channel().closeFuture().sync();
log.info(" 启动正在监听:{}",cf.channel().localAddress());
} finally {
group.shutdownGracefully().sync(); // 释放线程池资源
bossGroup.shutdownGracefully().sync();
}
} public static void main(String[] args) throws Exception { new NettyServer().start(); // 启动
}
}
package com.example.demohystrix.process;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.UnsupportedEncodingException; /**
* @author liusongwei
* @Title: NettyServerHandler
* @ProjectName claimfront
* @Description: TODO
* @date 2019/3/2216:31
*/
//标示一个ChannelHandler可以被多个Channel安全地共享
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
private static Logger log = LoggerFactory.getLogger(NettyServerHandler.class);
/*
* channel 通道 action 活跃的 回调调用
* 当客户端主动链接服务端的链接后,这个通道就是活跃的了。也就是客户端与服务端建立了通信通道并且可以传输数据
*/
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(ctx.channel().localAddress().toString() + " 通道已激活!");
} /*
* channelInactive
*
* channel 通道 Inactive 不活跃的
*
* 当客户端主动断开服务端的链接后,这个通道就是不活跃的。也就是说客户端与服务端的关闭了通信通道并且不可以传输数据
*
*/
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println(ctx.channel().localAddress().toString() + " 通道不活跃!");
// 关闭流
} /**
* TODO 此处用来处理收到的数据中含有中文的时 出现乱码的问题
*/
private String getMessage(ByteBuf buf) {
byte[] con = new byte[buf.readableBytes()];
buf.readBytes(con);
try {
return new String(con, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
} /**
* 功能:读取服务器发送过来的信息
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// 第一种:接收字符串时的处理
ByteBuf buf = (ByteBuf) msg;
String rev = getMessage(buf);
log.info("服务端收到的数据:" + rev);
//将接收到的消息写给发送者,而不冲刷出站消息
ctx.write("消息已接收");
} /**
* 功能:读取完毕客户端发送过来的数据之后的操作
*/
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("服务端接收数据完毕..");
// 第一种方法:写一个空的buf,并刷新写出区域。完成后关闭sock channel连接。
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
// ctx.flush();
// ctx.flush(); //
// 第二种方法:在client端关闭channel连接,这样的话,会触发两次channelReadComplete方法。
// ctx.flush().close().sync(); // 第三种:改成这种写法也可以,但是这中写法,没有第一种方法的好。
} /**
* 功能:服务端发生异常的操作
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
System.out.println("异常信息:\r\n" + cause.getMessage());
}
}
package com.example.demohystrix.process;

import java.net.InetSocketAddress;
import java.nio.charset.Charset; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* @author liusongwei
* @Title: NettyClient
* @ProjectName algorithmrsa
* @Description: TODO
* @date 2019/3/2216:37
*/
public class NettyClient {
private static Logger log = LoggerFactory.getLogger(NettyServer.class); private final String host;
private final int port; public NettyClient() {
this();
} public NettyClient(int port) {
this("localhost", port);
} public NettyClient(String host, int port) {
this.host = host;
this.port = port;
} public void start() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group) // 注册线程池
.channel(NioSocketChannel.class) // 使用NioSocketChannel来作为连接用的channel类
.remoteAddress(new InetSocketAddress(this.host, this.port)) // 绑定连接端口和host信息
.handler(new ChannelInitializer<SocketChannel>() { // 绑定连接初始化器
@Override
protected void initChannel(SocketChannel ch) throws Exception {
//ch.pipeline().addLast(new StringEncoder(Charset.forName("GBK")));
ch.pipeline().addLast(new StringEncoder(Charset.forName("GBK")));
ch.pipeline().addLast(new NettyClientHandler());
ch.pipeline().addLast(new ByteArrayEncoder());
ch.pipeline().addLast(new ChunkedWriteHandler()); }
});
ChannelFuture cf = b.connect().sync(); // 异步连接服务器
log.info("服务端连接成功...");
cf.channel().closeFuture().sync(); // 异步等待关闭连接channel
log.info("连接已关闭...");
} finally {
group.shutdownGracefully().sync(); // 释放线程池资源
}
} public static void main(String[] args) throws Exception {
new NettyClient("127.0.0.1", ).start(); // 连接127.0.0.1/65535,并启动
// new NettyClient("127.0.0.1", 8888).start(); // 连接127.0.0.1/65535,并启动 }
}
package com.example.demohystrix.process;

import java.nio.charset.Charset;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* @author liusongwei
* @Title: NettyClientHandler
* @ProjectName algorithmrsa
* @Description: TODO
* @date 2019/3/2216:40
*/
public class NettyClientHandler extends SimpleChannelInboundHandler<ByteBuf>{
private static Logger log = LoggerFactory.getLogger(NettyServer.class);
/**
* 向服务端发送数据
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
log.info("客户端与服务端通道-开启:" + ctx.channel().localAddress() + "channelActive");
String sendInfo = "Hello 这里是客户端 你好啊!";
ctx.writeAndFlush(Unpooled.copiedBuffer(sendInfo, CharsetUtil.UTF_8)); // 必须有flush
} /**
* 当客户端主动断开服务端的链接后,这个通道就是不活跃的。也就是说客户端与服务端的关闭了通信通道并且不可以传输数据
*/
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.info("客户端与服务端通道-关闭:" + ctx.channel().localAddress() + "channelInactive");
} @Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
System.out.println("读取客户端通道信息..");
ByteBuf buf = msg.readBytes(msg.readableBytes());
//log.info("收到信息:"+ByteBufUtil.hexDump(buf) + "; 数据包为:" + buf.toString(Charset.forName("utf-8")));
log.info("收到信息:"+ByteBufUtil.hexDump(buf) + "; 数据包为:" + buf.toString(Charset.forName("GBK")));
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
log.info("异常退出:" + cause.getMessage());
}
}

netty简单样例的更多相关文章

  1. extern外部方法使用C#简单样例

    外部方法使用C#简单样例 1.添加引用using System.Runtime.InteropServices; 2.声明和实现的连接[DllImport("kernel32", ...

  2. spring事务详解(二)简单样例

    系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.引子 ...

  3. velocity简单样例

    velocity简单样例整体实现须要三个步骤,详细例如以下: 1.创建一个Javaproject 2.导入须要的jar包 3.创建须要的文件 ============================= ...

  4. 自己定义隐式转换和显式转换c#简单样例

    自己定义隐式转换和显式转换c#简单样例 (出自朱朱家园http://blog.csdn.net/zhgl7688) 样例:对用户user中,usernamefirst name和last name进行 ...

  5. VC6 鼠标钩子 最简单样例

    Windows系统是建立在事件驱动的机制上的,说穿了就是整个系统都是通过消息的传递来实现的.而钩子是Windows系统中非常重要的系统接口,用它能够截获并处理送给其它应用程序的消息,来完毕普通应用程序 ...

  6. gtk+3.0的环境配置及基于gtk+3.0的python简单样例

    /*********************************************************************  * Author  : Samson  * Date   ...

  7. java 使用tess4j实现OCR的最简单样例

    网上很多教程没有介绍清楚tessdata的位置,以及怎么配置,并且对中文库的描述也存在问题,这里介绍一个最简单的样例. 1.使用maven,直接引入依赖,确保你的工程JDK是1.8以上 <dep ...

  8. 使用SALT-API进入集成开发的简单样例

    测试的时候,可以CURL -K,但真正作集成的时候,却是不可以的. 必须,不可以让TOKEN满天飞吧. 现在进入这个阶段了.写个样例先: import salt import salt.auth im ...

  9. VB.net数据库编程(03):一个SQLserver连接查询的简单样例

    这个样例,因为在ADO.net入门已经专门学了,再次进行复习 一下. 主要掌握连接字串的情况. 过程就是: 1.引用System.Data.SqlClient.而Access中引用 的是System. ...

随机推荐

  1. 搞清Image加载事件(onload)、加载状态(complete)后,实现图片的本地预览,并自适应于父元素内(完成)

    onload与complete介绍 complete只是HTMLImageElement对象的一个属性,可以判断图片加载完成,不管图片是不是有缓存:而onload则是这个Image对象的load事件回 ...

  2. [工具配置]使用requirejs模块化开发多页面一个入口js的使用方式

    描述 知道requirejs的都知道,每一个页面需要进行模块化开发都得有一个入口js文件进行模块配置.但是现在就有一个很尴尬的问题,如果页面很多的话,那么这个data-main对应的入口文件就会很多. ...

  3. BZOJ4559: [JLoi2016]成绩比较(dp 拉格朗日插值)

    题意 题目链接 Sol 想不到想不到.. 首先在不考虑每个人的真是成绩的情况下,设\(f[i][j]\)表示考虑了前\(i\)个人,有\(j\)个人被碾压的方案数 转移方程:\[f[i][j] = \ ...

  4. 2018-08-29 浏览器插件实现GitHub代码翻译原型演示

    此原型源自此想法: 中文化源码. 考虑到IDE插件工作量较大, 且与IDE绑定. 在代码转换工具的各种实现中, 综合考虑实用+易用+长远改进潜力, 浏览器插件似乎较有优势. 于是用最快捷的方式实现这一 ...

  5. 系统调用syscall---用户态切换到内核态的唯一途径

    1.应用程序有时需要内核协助完成一些处理,但是应用程序不可能执行内核代码(主要是安全性考虑), 那么,应用程序需要有一种机制告诉内核,它现在需要内核的帮助,这个机制就是系统调用. 2.系统调用的本质是 ...

  6. C# 利用SharpPcap实现网络包捕获嗅探

    本文是利用SharpPcap实现网络包的捕获的小例子,实现了端口监控,数据包捕获等功能,主要用于学习分享. 什么是SharpPcap? SharpPcap 是一个.NET 环境下的网络包捕获框架,基于 ...

  7. mumu模拟器安装xposed--如何在android模拟器上进行root

    问题描述 安装xposed表示failed to access root权限,新版的mumu模拟器没有了root选项,需要自己root. 1.先关掉应用兼容性,然后重启 电脑一般都是x86的,mumu ...

  8. 杨学明老师推出全新课程--《敏捷开发&IPD和敏捷开发结合的实践》

    课时:13小时(2天) 敏捷开发&IPD和敏捷开发结合的实践 讲  师:杨学明 [课程背景] 集成产品开发(IPD).集成能力成熟度模型(CMMI).敏捷开发(Agile Developmen ...

  9. Linux 中yum的配置

    1.进入yum的路径 cd /etc/yum.repos.d 2.将原始的repo文件移入一个新建的backup文件下做备份 mv CentOS* backup 3.在/etc/yum.repos.d ...

  10. Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库

    1.第一步,在本地数据库中建一个与服务器同名的数据库 2.第二步,右键源数据库,任务>导出数据,弹出导入导出提示框,点下一步继续 3.远程数据库操作,确认服务器名称(服务器地址).身份验证(输入 ...