标签:line 可靠 基于 read throws 开源 etc main 服务端
Netty是由JBOSS提供的一个java开源框架。Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。
也就是说,Netty 是一个基于NIO的客户、服务器端编程框架,使用Netty 可以确保你快速和简单的开发出一个网络应用,例如实现了某种协议的客户,服务端应用。Netty相当简化和流线化了网络应用的编程开发过程,例如,TCP和UDP的socket服务开发。
官网地址:http://netty.io/
Netty之所以能成为主流的NIO框架,是因为它有下面的优点:
我们编写一个服务端和客户端,客户端往服务端发送一条消息,消息传输先用字符串进行传递,服务端收到客户端发送的消息,然后回复一条消息。
首先编写服务端代码:
public class ImServer { public void run(int port) { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("decoder", new StringDecoder()); ch.pipeline().addLast("encoder", new StringEncoder()); ch.pipeline().addLast(new ServerStringHandler()); } }) .option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true); try { ChannelFuture f = bootstrap.bind(port).sync(); f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } } }
消息处理:
/** * 消息处理 */ public class ServerStringHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { System.err.println("server:" + msg.toString()); ctx.writeAndFlush(msg.toString() + "你好"); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); } }
启动服务,指定端口为8888:
public static void main(String[] args) { int port = 8888; new Thread(() -> { new ImServer().run(port); }).start(); }
编写客户端连接逻辑:
public class ImConnection { private Channel channel; public Channel connect(String host, int port) { doConnect(host, port); return this.channel; } private void doConnect(String host, int port) { EventLoopGroup workerGroup = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("decoder", new StringDecoder()); ch.pipeline().addLast("encoder", new StringEncoder()); ch.pipeline().addLast(new ClientStringHandler()); } }); ChannelFuture f = b.connect(host, port).sync(); channel = f.channel(); } catch(Exception e) { e.printStackTrace(); } } }
客户端消息处理:
/** * 当编解码器为字符串时用来接收数据 */ public class ClientStringHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { System.out.println("client:" + msg.toString()); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); } }
客户端启动入口,然后发送消息给服务端:
public static void main(String[] args) { String host = "127.0.0.1"; int port = 8888; Channel channel = new ImConnection().connect(host, port); channel.writeAndFlush("greenSniper"); }
测试步骤如下:
server:greenSniper
client:greenSniper你好
标签:line 可靠 基于 read throws 开源 etc main 服务端
原文地址:https://www.cnblogs.com/tangzhe/p/9398473.html