1.Netty入门
netty入门
1.netty介绍
(1)百度百科介绍:
netty是由jboss提供的一个java开源框架。netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。
也就是说,netty 是一个基于nio的客户、服务器端编程框架,使用netty 可以确保你快速和简单的开发出一个网络应用,例如实现了某种协议的客户、服务端应用。netty相当于简化和流线化了网络应用的编程开发过程,例如:基于tcp和udp的socket服务开发。
“快速”和“简单”并不用产生维护性或性能上的问题。netty 是一个吸收了多种协议(包括ftp、smtp、http等各种二进制文本协议)的实现经验,并经过相当精心设计的项目。最终,netty 成功的找到了一种方式,在保证易于开发的同时还保证了其应用的性能,稳定性和伸缩性。
(2)官网介绍:netty.io
netty is an asynchronous event-driven network application framework
for rapid development of maintainable high performance protocol servers & clients.
(3)参考资料:
- 参考书籍:netty实战(norman maurer & marvin allen wolfthal)
- 参考官方文档:https://netty.io/wiki/user-guide.html
- 参考网址:https://ifeve.com/tag/netty/
2.程序功能概述
客户端向服务器发送hellonetty-server!
服务器向客户端返回hello netty-client!
3.程序实现
-
服务端channelhandler
package com.xm.netty; 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 io.netty.util.charsetutil; public class serverhandler extends channelinboundhandleradapter { @override public void channelread(channelhandlercontext ctx, object msg) throws exception { bytebuf in = (bytebuf) msg; system.out.println("服务器接收:"+in.tostring(charsetutil.utf_8)); in.clear(); string str = "hello netty-client!"; in.writebytes(str.getbytes()); ctx.write(in); } @override public void channelreadcomplete(channelhandlercontext ctx) throws exception { ctx.writeandflush(unpooled.empty_buffer).addlistener(channelfuturelistener.close); } @override public void exceptioncaught(channelhandlercontext ctx, throwable cause) throws exception { cause.printstacktrace(); ctx.close(); } }
-
服务端server
package com.xm.netty; import java.net.inetsocketaddress; 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.nio.nioeventloopgroup; import io.netty.channel.socket.nio.nioserversocketchannel; public class server { private final int port; public server(int port) { this.port = port; } public static void main(string[] args) { int port = 8989; try { new server(port).start(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } private void start() throws interruptedexception { final serverhandler serverhandler = new serverhandler(); eventloopgroup group = new nioeventloopgroup(); try { serverbootstrap bootstrap = new serverbootstrap(); bootstrap .group(group) .channel(nioserversocketchannel.class) .localaddress(new inetsocketaddress( port)) .childhandler(new channelinitializer() { @override protected void initchannel(channel ch) throws exception { ch.pipeline().addlast(serverhandler); } }); channelfuture future = bootstrap.bind().sync(); future.channel().closefuture().sync(); } finally { group.shutdowngracefully().sync(); } } }
-
客户端channelhandler
package com.xm.netty; import io.netty.buffer.bytebuf; import io.netty.buffer.unpooled; import io.netty.channel.channelhandlercontext; import io.netty.channel.simplechannelinboundhandler; import io.netty.util.charsetutil; public class clienthandler extends simplechannelinboundhandler<bytebuf> { @override public void channelactive(channelhandlercontext ctx) throws exception { ctx.writeandflush(unpooled.copiedbuffer("hello netty-server!",charsetutil.utf_8)); } @override protected void channelread0(channelhandlercontext ctx, bytebuf msg) throws exception { system.out.println("客户端接收到消息:"+msg.tostring(charsetutil.utf_8)); } @override public void exceptioncaught(channelhandlercontext ctx, throwable cause) throws exception { cause.printstacktrace(); ctx.close(); } }
-
客户端client
package com.xm.netty; 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; public class client { private final int port; private final string host; public client(int port, string host) { this.port = port; this.host = host; } public static void main(string[] args) { string host = "127.0.0.1"; int port = 8989; try { new client(port, host).start(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } private void start() throws interruptedexception { eventloopgroup group = new nioeventloopgroup(); try { bootstrap bootstrap = new bootstrap(); bootstrap .group(group) .channel(niosocketchannel.class) .remoteaddress(host, port) .handler(new channelinitializer<socketchannel>() { @override protected void initchannel(socketchannel ch) throws exception { ch.pipeline().addlast(new clienthandler()); } }); channelfuture future = bootstrap.connect().sync(); future.channel().closefuture().sync(); } finally { group.shutdowngracefully().sync(); } } }
-
导入依赖
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.xm</groupid> <artifactid>netty</artifactid> <version>0.0.1-snapshot</version> <dependencies> <dependency> <groupid>io.netty</groupid> <artifactid>netty-all</artifactid> <version>4.1.16.final</version> </dependency> </dependencies> </project>
4.运行结果:
-
服务端:
服务器接收:hello netty-server!
-
客户端:
客户端接收到消息:hello netty-client!
上一篇: angular2使用简单介绍
下一篇: php通过递归方式复制目录和子目录的方法