List of usage examples for io.netty.channel ChannelInitializer ChannelInitializer
ChannelInitializer
From source file:com.doctor.netty5.ebook.netty_the_definitive_guide.Chapter4ForTimerClient.java
License:Apache License
public void connect(String host, int port) throws InterruptedException { EventLoopGroup group = new NioEventLoopGroup(); try {/* w ww.j a v a 2 s.c o m*/ Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LineBasedFrameDecoder(2014)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new TimeClientHander()); } }); ChannelFuture f = b.connect(host, port).sync(); f.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } }
From source file:com.doctor.netty5.ebook.netty_the_definitive_guide.Chapter4ForTimerServer.java
License:Apache License
public void bind(int port) throws InterruptedException { // ??NIO//from w w w . j a v a 2 s . c om EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LineBasedFrameDecoder(1024)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new TimeServerHander()); } }); ChannelFuture future = b.bind(port).sync(); future.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.ebook.netty_the_definitive_guide.TimerClient.java
License:Apache License
public void connect(String host, int port) throws InterruptedException { EventLoopGroup group = new NioEventLoopGroup(); try {//from w w w. j a va 2s . c om Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeClientHander()); } }); ChannelFuture f = b.connect(host, port).sync(); f.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } }
From source file:com.doctor.netty5.ebook.netty_the_definitive_guide.TimeServer.java
License:Apache License
public void bind(int port) throws InterruptedException { // ??NIO/*w w w . j a v a2 s. c o m*/ EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeServerHander()); } }); ChannelFuture future = b.bind(port).sync(); future.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.chatroom.ChatRoomClient.java
License:Apache License
public void start() throws InterruptedException { NioEventLoopGroup workersGroup = new NioEventLoopGroup(1); try {/*from w ww. j a v a2 s. c om*/ Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workersGroup).channel(NioSocketChannel.class).remoteAddress(host, port) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast( new DelimiterBasedFrameDecoder(8049, true, Delimiters.lineDelimiter())); ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new ChatRoomClientHandler()); } }); ChannelFuture channelFuture = bootstrap.connect().sync(); channelFuture.channel().closeFuture().sync(); } finally { workersGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.chatroom.ChatRoomServer.java
License:Apache License
public void start() throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try {//w w w . ja v a 2 s . co m bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast( new DelimiterBasedFrameDecoder(8049, true, Delimiters.lineDelimiter())); ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new ChatRoomServerHandler()); } }); ChannelFuture channelFuture = bootstrap.bind().sync(); System.out.println(ChatRoomServer.class.getName() + " started and listen on port:" + channelFuture.channel().localAddress()); channelFuture.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.echo.EchoClient.java
License:Apache License
public void start() throws InterruptedException { NioEventLoopGroup workersGroup = new NioEventLoopGroup(1); try {//from w w w.ja v a 2s . c o m Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workersGroup).channel(NioSocketChannel.class).remoteAddress(host, port) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LineBasedFrameDecoder(2048)); ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new EchoClientHandler()); } }); ChannelFuture channelFuture = bootstrap.connect().sync(); channelFuture.channel().closeFuture().sync(); } finally { workersGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.echo.EchoServer.java
License:Apache License
public void start() throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w ww. ja va 2 s . co m bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LineBasedFrameDecoder(2048)); ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new EchoServerHandler()); } }); ChannelFuture channelFuture = bootstrap.bind().sync(); System.out.println(EchoServer.class.getName() + " started and listen on port:" + channelFuture.channel().localAddress()); channelFuture.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.echo_object.EchoClient.java
License:Apache License
public void start() throws InterruptedException { NioEventLoopGroup workersGroup = new NioEventLoopGroup(1); try {/* ww w . java 2 s . co m*/ Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workersGroup).channel(NioSocketChannel.class).remoteAddress(host, port) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ObjectEncoder()); ch.pipeline().addLast(new ObjectDecoder( ClassResolvers.softCachingResolver(this.getClass().getClassLoader()))); ch.pipeline().addLast(new EchoClientHandler()); } }); ChannelFuture channelFuture = bootstrap.connect().sync(); channelFuture.channel().closeFuture().sync(); } finally { workersGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.echo_object.EchoServer.java
License:Apache License
public void start() throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w ww.jav a 2s.co m*/ bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ObjectEncoder()); ch.pipeline().addLast(new ObjectDecoder( ClassResolvers.softCachingResolver(this.getClass().getClassLoader()))); ch.pipeline().addLast(new EchoServerHandler()); } }); ChannelFuture channelFuture = bootstrap.bind().sync(); System.out.println(EchoServer.class.getName() + " started and listen on port:" + channelFuture.channel().localAddress()); channelFuture.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }