Practical Netty (2) CS模式下的Echo及String与ChannelBuffer的转化
1. Echo TCP Server
Netty 服务器写多了之后就知道,主要的不同就在于 Handler 的实现。当然 Bootstrap 的不同使用也是有所影响的。
/**
* EchoTcpServer.java
*/
package test.netty;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
public class EchoTcpServer {
private final int port;
public EchoTcpServer(int port) {
this.port = port;
}
public void run() {
ServerBootstrap sb = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
sb.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(new EchoTcpServerHandler());
}
});
sb.bind(new InetSocketAddress(port));
}
public static void main(String[] args) {
new EchoTcpServer(28080).run();
}
}
最关键的自然是 Handler。EchoTcpServerHandler
继承SimpleChannelHandler
,覆盖messageReceived
,参数MessageEvent.getMessage()
可以得到 Client 发送来的消息,再调用e.getChannel().write(…)
写会给 Client。
/**
* EchoTcpServerHandler.java
*/
package test.netty;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
public class EchoTcpServerHandler extends SimpleChannelHandler {
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
e.getChannel().write(e.getMessage());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
e.getCause().printStackTrace();
e.getChannel().close();
}
}
该 EchoTcpServer,可以通过 Telnet 做客户端来访问。
2. String 与 ChannelBuffer 相互转化
通过ChannelBuffers
创建一个ChannelBuffer
,然后写入数据。最后再把这个ChannelBuffer
写到Channel
里。
String msg = "Hello, I'm client.";
ChannelBuffer buffer = ChannelBuffers.buffer(msg.length());
buffer.writeBytes(msg.getBytes());
e.getChannel().write(buffer);
上面MessageEvent.getMessage()
得到的是Object
,它是一个ChannelBuffer
,由于 Client 发送来的是 String,所以它其实是一个 String 啦。怎么把这个 String 给打印到 Server 的终端?用如下的方法:
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
System.out.println(buffer.toString(Charset.defaultCharset()));
}
Reference
- http://www.coderli.com/netty-string-channelbuffer
- http://netty.io
-
转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant,微博:weibo.com/lauginhom
-
分享到:
相关推荐
基于Netty的CS模式通信模块代码,单机并发1万。
《Netty实战》中的 echo 代码-netty-echo
基于EchoServer对Netty4.1源码进行分析,水平有限,如果有问题大家可以提出来共同分享。
Netty 权威指南,完整Netty 权威指南,完整Netty 权威指南,完整Netty 权威指南,完整
Netty权威 第2版 带书签目录 RPC dubbo Netty权威 第2版 带书签目录 RPC dubbo Netty权威 第2版 带书签目录 RPC dubbo Netty权威 第2版 带书签目录 RPC dubbo Netty权威 第2版 带书签目录 RPC dubbo Netty权威 第2版...
Netty服务端与客户端,数据的发送与接收 使用:先运行TimeServer,在运行TimeClient。成功连接后,服务器发送一个时间给客户端。输出到客户端控制台 4.第三个示例 com.user_1 Netty将java对象作为数据的发送与...
64_Channel与ChannelPipeline关联关系及模式运用 65_ChannelPipeline创建时机与高级拦截过滤器模式的运用 66_Netty常量池实现及ChannelOption与Attribute作用分析 67_Channel与ChannelHandler及...
第64讲:Channel与ChannelPipeline关联关系及模式运用 第65讲:ChannelPipeline创建时机与高级拦截过滤器模式的运用 第66讲:Netty常量池实现及ChannelOption与Attribute作用分析 第67讲:Channel与...
Netty (netty-netty-5.0.0.Alpha2.tar.gz)是一个 NIO 客户端服务器框架,可以快速轻松地开发协议服务器和客户端等网络应用程序。它极大地简化和流线了网络编程,例如 TCP 和 UDP 套接字服务器。 “快速和简单”并...
此资源为websocket的页面源码,配合Spring boot 2基于Netty的高性能Websocket服务器(心跳模式) 文章来使用
norbert是一个方便做CS模式的集群的JAVA API,norbert封装了zookeeper和netty,使用了协议栈缓存,基于SCALA开发 示例代码: public class NorbertClient { public static void main(String[] args) { Cluster...
跟闪电侠学Netty:Netty即时聊天实战与底层原理-book-netty
Netty4实现的echo client和server,导入myeclipse即可。
Netty 5.0.0.Alpha2 版本,包含:Jar包+源代码+API文档。 官网Netty 5.0版本已经废弃,所以发布出来,方便大家下载。 《Netty权威指南》一书使用的就是Netty 5.0
netty 5.0.0 Alpha2,netty新版本,修复alpha1的很多bug
Z00317 NETTY权威指南(第2版)
绍如何搭建一个准实时聊天问答程序,包括微信小程序和H5网页版。...该项目服务端主要使用了Java + Spring Boot + Netty + WebSocket等技术栈,聊天客户端使用的是UniApp来轻松搭建微信小程序和H5网页端。
下载地址:... netty-all-5.0.0.Alpha2.jar netty-all-5.0.0.Alpha2-sources.jar netty-all-5.0.0.Alpha2-javadoc.jar netty-example-5.0.0.Alpha2.jar netty-example-5.0.0.Alpha2-sources.jar 等
本书深入剖析了Netty,更全面系统讲解底层架构、实践与源码,能够让读者更清晰地理解Netty 架构设计理念,第2版增加了MessagePack 编解码、服务端创建、客户端创建、高性能之道、可靠性、安全性等内容,内容更精彩,...