首页 / NETTY / netty入门(1)
netty入门(1)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了netty入门(1),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4301字,纯文字阅读大概需要7分钟。
内容图文



public static void main(String[] args) { String host = "127.0.0.1"; int port = 8886; EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .handler(new MyClientInitializer()); // 启动客户端. ChannelFuture f = b.connect(host, port).sync(); f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { group.shutdownGracefully(); } } publicclass MyClientInitializer extends ChannelInitializer<SocketChannel> { @Override protectedvoid initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); pipeline.addLast(new LengthFieldPrepender(4)); pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8)); pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8)); pipeline.addLast(new MyClientHandler()); } } publicclass MyClientHandler extends SimpleChannelInboundHandler<String> { @Override protectedvoid channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { System.out.println(ctx.channel().remoteAddress()); System.out.println("msg:"+msg); ctx.writeAndFlush("from client:" + LocalDateTime.now()); } @Override publicvoid channelActive(ChannelHandlerContext ctx) throws Exception { ctx.writeAndFlush("from client:" + "我主动发第一条数据"); } @Override publicvoid exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } }
public class MyServer { int port ; public MyServer(int port){ this.port = port; } publicvoid start() throws Exception{ EventLoopGroup boss = new NioEventLoopGroup(); // selector EventLoopGroup work = new NioEventLoopGroup(); // channeltry { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(boss,work) .handler(new LoggingHandler(LogLevel.DEBUG)) .channel(NioServerSocketChannel.class) .childHandler(new MyServerInitializer()); ChannelFuture f = bootstrap.bind(new InetSocketAddress(port)).sync(); System.out.println(" server start up on port : " + port); f.channel().closeFuture().sync(); } finally { boss.shutdownGracefully(); work.shutdownGracefully(); } } publicstaticvoid main(String[] args) throws Exception{ MyServer server = new MyServer(8899);// 8081为启动端口 server.start(); } } publicclass MyServerInitializer extends ChannelInitializer<SocketChannel> { // 客户端一旦和服务端连接,这个方法就会被调用 @Override protectedvoid initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); pipeline.addLast(new LengthFieldPrepender(4)); pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8)); pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8)); pipeline.addLast(new MyRequestHandler()); } } publicclass MyRequestHandler extends SimpleChannelInboundHandler<String> { @Override protectedvoid channelRead0(ChannelHandlerContext ctx, String req) throws Exception { System.out.println(ctx.channel().remoteAddress()); System.out.println("req msg:"+req); ctx.writeAndFlush("from server:" + LocalDateTime.now()); } }
原文:https://www.cnblogs.com/shineipangtuo/p/13763507.html
内容总结
以上是互联网集市为您收集整理的netty入门(1)全部内容,希望文章能够帮你解决netty入门(1)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。