Netty是一个Java开源框架,用于传输数据。由server和client组成,封装了Java nio,支持TCP, UDP等协议。这里写了一Demo

EchoClientHandler.java
package chapter1;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil; @ChannelHandler.Sharable
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> { @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(Unpooled.copiedBuffer("hello guanxianseng", CharsetUtil.UTF_8));
} protected void messageReceived(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
System.out.println("Client received:" + msg.toString(CharsetUtil.UTF_8));
System.out.println(msg.toString());
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
} @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("Client received:" + msg.toString()); }
}
EchoServer.java
package chapter1;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel; import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit; public class EchoServer {
private int port = 1234; public EchoServer(int port) {
this.port = port;
} public static void main(String[] args) throws Exception{
int port = 1234;
EchoServer echoServer = new EchoServer(port);
schedulTask();
echoServer.start(); } public void start() throws Exception{
final EchoServerHandler serverHandler = new EchoServerHandler();
EventLoopGroup group = new NioEventLoopGroup();
try{
ServerBootstrap b = new ServerBootstrap();
b.group(group)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(serverHandler);
}
});
ChannelFuture channelFuture = b.bind().sync();
System.out.println("sync test1");
channelFuture.channel().closeFuture().sync();
System.out.println("sync test2"); } catch (Exception e){ }finally {
group.shutdownGracefully().sync();
System.out.println("sync test3");
}
} public static void schedulTask(){
System.out.println("before test");
Channel ch = new EmbeddedChannel();
ch.eventLoop().scheduleAtFixedRate(new Runnable() {
public void run() {
System.out.println("test");
}
}, 0, 5, TimeUnit.SECONDS);
}
}
EchoClientHandler.java
package chapter1;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil; @ChannelHandler.Sharable
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> { @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(Unpooled.copiedBuffer("hello guanxianseng", CharsetUtil.UTF_8));
} protected void messageReceived(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
System.out.println("Client received:" + msg.toString(CharsetUtil.UTF_8));
System.out.println(msg.toString());
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
} @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("Client received:" + msg.toString()); }
}
EchoClient.java
package chapter1;

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 java.net.InetSocketAddress; public class EchoClient {
private String host;
private int port; public EchoClient(String host, int port) {
this.host = host;
this.port = port;
} public static void main(String[] args) throws Exception {
int port = 1234;
String host = "127.0.0.1";
EchoClient echoClient = new EchoClient(host, port);
echoClient.start();
} public void start() throws Exception{
EventLoopGroup group = new NioEventLoopGroup();
try{
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoClientHandler());
}
});
ChannelFuture f = b.connect().sync();
f.channel().closeFuture().sync(); }catch (Exception e){
e.printStackTrace();
}finally {
group.shutdownGracefully().sync();
}
}
}

netty如何实现各种回调,怎么发送、接收消息,还要看下源码

最新文章

  1. Linux安全基础:grep命令的使用
  2. 微信小程序-表单组件
  3. python MySQLdb中文乱码
  4. C# RSA
  5. c语言结构体4之结构体引用
  6. WP8中的地图和导航
  7. RabbitMQ核心概念篇
  8. html5判断浏览器来源并跳转
  9. 浅谈编程语言中的新宠Python,你叫它如何不火?
  10. win10装ubuntu双系统
  11. Python的基本语法1
  12. webpack的安装及使用
  13. LOJ #2142. 「SHOI2017」相逢是问候(欧拉函数 + 线段树)
  14. python 实现统计ftp服务器指定目录下文件夹数目、文件数目及所有文件大小
  15. 使用gulp和bable实现实时编译ES6代码
  16. [C++]栈区(栈)与堆区(类链表)[转/摘]
  17. linux守护进程与&amp;的区别
  18. (转)在.NET程序运行过程中,什么是堆,什么是栈?什么情况下会在堆(栈)上分配数据?它们有性能上的区别吗?“结构”对象可能分配在堆上吗?什么情况下会发生,有什么需要注意的吗?
  19. lis nlogn算法
  20. shiro实战系列(十四)之配置

热门文章

  1. 【bzoj4176】Lucas的数论 莫比乌斯反演+杜教筛
  2. spark-2.2.1在centos7安装
  3. AVFoundation 文本播报
  4. Spring学习笔记(六)—— SSH整合
  5. 三元运算符,i++(先用后加) ++i (先加后用)区别
  6. CentOS 7 开放防火墙端口 命令(转载)
  7. Party All the Time(三分)
  8. Python-list()之remove()/pop() I /del()
  9. Linux(1)-CentOS7下解决ifconfig command not found
  10. flex布局在ios8上的兼容性问题