admin 管理员组文章数量: 887021
2024年1月16日发(作者:js修炼书)
/* * Copyright 2012 The Netty Project * * The Netty Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at: * * /licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */package ;import f;import le;import lHandlerContext;import lInboundHandlerAdapter;/** * Handler implementation for the echo server. */@Sharablepublic class EchoServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { (msg); if(msg instanceof ByteBuf){ ByteBuf byteBuf = (ByteBuf) msg; int i = leBytes(); byte[] bytes = new byte[i]; es(0,bytes); n(new String(bytes)); } } @Override public void channelReadComplete(ChannelHandlerContext ctx) { (); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // Close the connection when an exception is raised. tackTrace(); (); }}客户端启动类:/* * Copyright 2012 The Netty Project * * The Netty Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at: * * /licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations
* License for the specific language governing permissions and limitations * under the License. */package ;import rap;import lFuture;import lInitializer;import lOption;import lPipeline;import oopGroup;import ntLoopGroup;import Channel;import ketChannel;import text;import textBuilder;import reTrustManagerFactory;/** * Sends one message when a connection is open and echoes back any received * data to the server. Simply put, the echo client initiates the ping-pong * traffic between the echo client and server by sending the first message to * the server. */public final class EchoClient { static final boolean SSL = perty("ssl") != null; static final String HOST = perty("host", "127.0.0.1"); static final int PORT = nt(perty("port", "8007")); static final int SIZE = nt(perty("size", "26")); public static void main(String[] args) throws Exception { // Configure final SslContext sslCtx; if (SSL) { sslCtx = ent() .trustManager(CE).build(); } else { sslCtx = null; } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); (group) .channel() .option(_NODELAY, true) .handler(new ChannelInitializer
@Override public void channelReadComplete(ChannelHandlerContext ctx) { (); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // Close the connection when an exception is raised. tackTrace(); (); }}
版权声明:本文标题:netty基本概念及入门示例代码 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1705371327h482996.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论