设为首页 加入收藏

TOP

Java NIO框架--Netty4的简单示例(二)
2015-02-02 14:24:03 来源: 作者: 【 】 浏览:24
Tags:Java NIO 框架 --Netty4 简单 示例
ds ChannelInboundHandlerAdapter {
?
? ? @Override
? ? public void channelRead(ChannelHandlerContext ctx, Object msg) {
? ? ? ? ctx.write("server write msg:"+msg);
? ? }
?
? ? @Override
? ? public void channelReadComplete(ChannelHandlerContext ctx) {
? ? ? ? ctx.flush();
? ? }
?
? ? @Override
? ? public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
? ? ? ? // Close the connection when an exception is raised.
? ? ? ? cause.printStackTrace();
? ? ? ? ctx.close();
? ? }
}


?


?


import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
?
?
/**
?* DateTime: 2015年1月5日 上午9:56:22
?*
?*/
public class HelloWorldClient {
? ? static final boolean SSL = System.getProperty("ssl") != null;
? ? static final String HOST = System.getProperty("host", "127.0.0.1");
? ? static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));
?
?
? ? public static void main(String[] args) throws Exception {
? ? ? ? // Configure SSL.git
? ? ? ? final SslContext sslCtx;
? ? ? ? if (SSL) {
? ? ? ? ? ? sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
? ? ? ? }
? ? ? ? else {
? ? ? ? ? ? sslCtx = null;
? ? ? ? }
?
? ? ? ? // Configure the client.
? ? ? ? EventLoopGroup group = new NioEventLoopGroup();
? ? ? ? try {
? ? ? ? ? ? Bootstrap b = new Bootstrap();
? ? ? ? ? ? b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
? ? ? ? ? ? ? ? .handler(new ChannelInitializer() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void initChannel(SocketChannel ch) throws Exception {
? ? ? ? ? ? ? ? ? ? ? ? ChannelPipeline p = ch.pipeline();
? ? ? ? ? ? ? ? ? ? ? ? if (sslCtx != null) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? p.addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
? ? ? ? ? ? ? ? ? ? ? ? ? ? new HelloWorldClientHandler());
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
?
? ? ? ? ? ? // Start the client.
? ? ? ? ? ? ChannelFuture f = b.connect(HOST, PORT).sync();
?
? ? ? ? ? ? // Wait until the connection is closed.
? ? ? ? ? ? f.channel().closeFuture().sync();
? ? ? ? }
? ? ? ? finally {
? ? ? ? ? ? // Shut down the event loop to terminate all threads.
? ? ? ? ? ? group.shutdownGracefully();
? ? ? ? }
? ? }
}
?
?
class HelloWorldClientHandler extends ChannelInboundHandlerAdapter {
?
? ? private final String msg = "hello java world";
?
?
? ? /**
? ? * Creates a client-side handler.
? ? */
? ? public HelloWorldClientHandler() {
? ? ? ? //TODO
? ? }
?
?
? ? @Override
? ? public void channelActive(ChannelHandlerContext ctx) {
? ? ? ? ctx.writeAndFlush(msg);
? ? }
?
?
? ? @Override
? ? public void channelRead(ChannelHandlerContext ctx, Object msg) {
? ? ? ? System.out.println(msg);
? ? ? ? //? ? ? ? ctx.write(msg);
? ? }
?
?
? ? @Override
? ? public void channelReadComplete(C

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Python版简易计算器的实现 下一篇Java多线程--信号量(Semaphore)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: