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 initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ne(); if (sslCtx != null) { t(dler((), HOST, PORT)); } //t(new LoggingHandler()); t(new EchoClientHandler()); } }); // Start the client. ChannelFuture f = t(HOST, PORT).sync(); // Wait until the connection is closed. l().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads.

@Override public void channelReadComplete(ChannelHandlerContext ctx) { (); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // Close the connection when an exception is raised. tackTrace(); (); }}


本文标签: 修炼 客户端 作者 启动