└── src └── com └── example └── netty ├── LengthBasedFrameEncoder.java ├── EchoServer.java ├── FileClient.java ├── FileServer.java └── EchoClient.java /src/com/example/netty/LengthBasedFrameEncoder.java: -------------------------------------------------------------------------------- 1 | package com.example.netty; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | import io.netty.buffer.Unpooled; 5 | import io.netty.channel.ChannelHandlerContext; 6 | import io.netty.handler.codec.MessageToMessageEncoder; 7 | 8 | import java.nio.ByteBuffer; 9 | import java.util.List; 10 | 11 | public class LengthBasedFrameEncoder extends MessageToMessageEncoder { 12 | 13 | @Override 14 | protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List list) throws Exception { 15 | ByteBuf byteBuf = Unpooled.buffer(4 + msg.readableBytes()); 16 | byteBuf.writeBytes(ByteBuffer.allocate(4).putInt(msg.readableBytes()).array()); 17 | byteBuf.writeBytes(msg); 18 | list.add(byteBuf); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /src/com/example/netty/EchoServer.java: -------------------------------------------------------------------------------- 1 | package com.example.netty; 2 | 3 | import io.netty.bootstrap.ServerBootstrap; 4 | import io.netty.channel.*; 5 | import io.netty.channel.nio.NioEventLoopGroup; 6 | import io.netty.channel.socket.SocketChannel; 7 | import io.netty.channel.socket.nio.NioServerSocketChannel; 8 | import io.netty.handler.codec.LengthFieldBasedFrameDecoder; 9 | import io.netty.handler.codec.string.StringDecoder; 10 | import io.netty.handler.codec.string.StringEncoder; 11 | 12 | public class EchoServer { 13 | 14 | public static void main(String args[]) throws Exception{ 15 | EventLoopGroup bossGroup = new NioEventLoopGroup(1); 16 | EventLoopGroup workerGroup = new NioEventLoopGroup(); 17 | 18 | try { 19 | ServerBootstrap b = new ServerBootstrap(); 20 | b.group(bossGroup, workerGroup) 21 | .channel(NioServerSocketChannel.class) 22 | .childHandler(new ChannelInitializer() { 23 | 24 | @Override 25 | protected void initChannel(SocketChannel channel) throws Exception { 26 | ChannelPipeline p = channel.pipeline(); 27 | p.addLast(new LengthFieldBasedFrameDecoder(2000, 0, 4, 0, 4)); 28 | p.addLast(new StringDecoder()); 29 | p.addLast(new LengthBasedFrameEncoder()); 30 | p.addLast(new StringEncoder()); 31 | p.addLast(new EchoServerHandler()); 32 | } 33 | }); 34 | 35 | ChannelFuture f = b.bind(1221).sync(); 36 | f.channel().closeFuture().sync(); 37 | 38 | } finally { 39 | workerGroup.shutdownGracefully(); 40 | bossGroup.shutdownGracefully(); 41 | } 42 | } 43 | 44 | static class EchoServerHandler extends ChannelInboundHandlerAdapter { 45 | 46 | @Override 47 | public void channelRead(ChannelHandlerContext channelHandlerContext, Object obj) throws Exception { 48 | String val = (String)obj; 49 | System.out.println(val); 50 | 51 | channelHandlerContext.writeAndFlush(val); 52 | } 53 | 54 | @Override 55 | public void channelReadComplete(ChannelHandlerContext channelHandlerContext) { 56 | // channelHandlerContext.flush(); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/com/example/netty/FileClient.java: -------------------------------------------------------------------------------- 1 | package com.example.netty; 2 | 3 | import io.netty.bootstrap.Bootstrap; 4 | import io.netty.channel.*; 5 | import io.netty.channel.nio.NioEventLoopGroup; 6 | import io.netty.channel.socket.SocketChannel; 7 | import io.netty.channel.socket.nio.NioSocketChannel; 8 | import io.netty.handler.logging.LoggingHandler; 9 | import io.netty.handler.stream.ChunkedFile; 10 | import io.netty.handler.stream.ChunkedWriteHandler; 11 | 12 | import java.io.File; 13 | 14 | public class FileClient { 15 | 16 | public static void main(String args[]) throws Exception{ 17 | EventLoopGroup workerGroup = new NioEventLoopGroup(); 18 | 19 | try { 20 | Bootstrap b = new Bootstrap(); 21 | b.group(workerGroup) 22 | .channel(NioSocketChannel.class) 23 | .handler(new ChannelInitializer() { 24 | 25 | @Override 26 | protected void initChannel(SocketChannel channel) throws Exception { 27 | ChannelPipeline p = channel.pipeline(); 28 | p.addLast(new LoggingHandler()); 29 | p.addLast(new ChunkedWriteHandler()); 30 | p.addLast(new FileClientHandler()); 31 | } 32 | }); 33 | 34 | ChannelFuture f = b.connect("localhost", 1221).sync(); 35 | f.channel().closeFuture().sync(); 36 | 37 | } finally { 38 | workerGroup.shutdownGracefully(); 39 | } 40 | } 41 | 42 | static class FileClientHandler extends ChannelInboundHandlerAdapter { 43 | 44 | @Override 45 | public void channelActive(ChannelHandlerContext ctx) throws Exception{ 46 | ctx.writeAndFlush(new ChunkedFile(new File("/Users/tushar_v_roy/Desktop/interview/interview.iws"))); 47 | } 48 | 49 | @Override 50 | public void channelRead(ChannelHandlerContext channelHandlerContext, Object obj) throws Exception { 51 | String val = (String)obj; 52 | System.out.println(val); 53 | 54 | channelHandlerContext.writeAndFlush(val); 55 | } 56 | 57 | 58 | @Override 59 | public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable e) { 60 | System.out.println("Exception"); 61 | // channelHandlerContext.close(); 62 | // channelHandlerContext.flush(); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/com/example/netty/FileServer.java: -------------------------------------------------------------------------------- 1 | package com.example.netty; 2 | 3 | import io.netty.bootstrap.ServerBootstrap; 4 | import io.netty.buffer.ByteBuf; 5 | import io.netty.channel.*; 6 | import io.netty.channel.nio.NioEventLoopGroup; 7 | import io.netty.channel.socket.SocketChannel; 8 | import io.netty.channel.socket.nio.NioServerSocketChannel; 9 | import io.netty.handler.logging.LoggingHandler; 10 | 11 | import java.io.FileOutputStream; 12 | 13 | public class FileServer { 14 | 15 | public static void main(String args[]) throws Exception{ 16 | EventLoopGroup bossGroup = new NioEventLoopGroup(1); 17 | EventLoopGroup workerGroup = new NioEventLoopGroup(); 18 | 19 | try { 20 | ServerBootstrap b = new ServerBootstrap(); 21 | b.group(bossGroup, workerGroup) 22 | .channel(NioServerSocketChannel.class) 23 | .handler(new LoggingHandler()) 24 | .childHandler(new ChannelInitializer() { 25 | 26 | @Override 27 | protected void initChannel(SocketChannel channel) throws Exception { 28 | 29 | ChannelPipeline p = channel.pipeline(); 30 | p.addLast(new LoggingHandler()); 31 | // p.addLast(new ChunkedWriteHandler()); 32 | p.addLast(new FileServerHandler()); 33 | } 34 | }); 35 | 36 | ChannelFuture f = b.bind(1221).sync(); 37 | f.channel().closeFuture().sync(); 38 | 39 | } finally { 40 | workerGroup.shutdownGracefully(); 41 | bossGroup.shutdownGracefully(); 42 | } 43 | } 44 | 45 | static class FileServerHandler extends ChannelInboundHandlerAdapter { 46 | 47 | FileOutputStream fileOutputStream; 48 | FileServerHandler() throws Exception{ 49 | fileOutputStream = new FileOutputStream("/tmp/test"); 50 | } 51 | @Override 52 | public void channelRead(ChannelHandlerContext channelHandlerContext, Object obj) throws Exception { 53 | ByteBuf byteBuf = (ByteBuf)obj; 54 | byteBuf.readBytes(fileOutputStream, byteBuf.readableBytes()); 55 | 56 | // channelHandlerContext.writeAndFlush(val); 57 | } 58 | 59 | @Override 60 | public void channelReadComplete(ChannelHandlerContext channelHandlerContext) { 61 | System.out.println("read complete"); 62 | // channelHandlerContext.flush(); 63 | } 64 | 65 | @Override 66 | public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable e) { 67 | System.out.println("Exception"); 68 | e.printStackTrace(); 69 | // channelHandlerContext.close(); 70 | // channelHandlerContext.flush(); 71 | } 72 | 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/com/example/netty/EchoClient.java: -------------------------------------------------------------------------------- 1 | package com.example.netty; 2 | 3 | import io.netty.bootstrap.Bootstrap; 4 | import io.netty.buffer.ByteBuf; 5 | import io.netty.buffer.Unpooled; 6 | import io.netty.channel.*; 7 | import io.netty.channel.nio.NioEventLoopGroup; 8 | import io.netty.channel.socket.SocketChannel; 9 | import io.netty.channel.socket.nio.NioSocketChannel; 10 | import io.netty.handler.codec.LengthFieldBasedFrameDecoder; 11 | import io.netty.handler.codec.string.StringDecoder; 12 | import io.netty.handler.codec.string.StringEncoder; 13 | 14 | import java.nio.ByteBuffer; 15 | import java.util.Objects; 16 | import java.util.concurrent.Executor; 17 | import java.util.concurrent.Executors; 18 | import java.util.concurrent.atomic.AtomicInteger; 19 | 20 | public class EchoClient { 21 | 22 | static String msg = "HELLO_WORLD_HELLO_WORLD_HELLO_WORLD_HELLO_WORLD_HELLO_WORLD_HELLO_WORLD_HELLO_WORLD_HELLO_WORLD"; 23 | 24 | public static void main(String args[]) throws Exception{ 25 | EventLoopGroup group = new NioEventLoopGroup(); 26 | try { 27 | Bootstrap b = new Bootstrap(); 28 | b.group(group) 29 | .channel(NioSocketChannel.class) 30 | .handler(new ChannelInitializer() { 31 | 32 | @Override 33 | protected void initChannel(SocketChannel socketChannel) throws Exception { 34 | ChannelPipeline p = socketChannel.pipeline(); 35 | p.addLast(new LengthFieldBasedFrameDecoder(2000, 0, 4, 0, 4)); 36 | p.addLast(new StringDecoder()); 37 | p.addLast(new LengthBasedFrameEncoder()); 38 | p.addLast(new StringEncoder()); 39 | p.addLast(new EchoClientHandler()); 40 | } 41 | }); 42 | ChannelFuture future = b.connect("localhost", 1221).sync(); 43 | future.channel().closeFuture().sync(); 44 | } finally { 45 | group.shutdownGracefully(); 46 | } 47 | } 48 | 49 | static class EchoClientHandler extends ChannelInboundHandlerAdapter { 50 | 51 | @Override 52 | public void channelActive(ChannelHandlerContext ctx) { 53 | GenerateTraffic gt = new GenerateTraffic(ctx); 54 | gt.start(); 55 | } 56 | 57 | @Override 58 | public void channelRead(ChannelHandlerContext ctx, Object msg) { 59 | System.out.println((String) msg); 60 | } 61 | } 62 | 63 | static class GenerateTraffic { 64 | ChannelHandlerContext ctx; 65 | Executor executor; 66 | AtomicInteger i = new AtomicInteger(0); 67 | GenerateTraffic(ChannelHandlerContext ctx){ 68 | this.ctx = ctx; 69 | executor = Executors.newFixedThreadPool(10); 70 | } 71 | 72 | public void start() { 73 | executor.execute(() -> { 74 | while(true) { 75 | ctx.writeAndFlush(msg + i.get()); 76 | i.incrementAndGet(); 77 | try { 78 | Thread.sleep(10); 79 | } catch (InterruptedException e) { 80 | throw new RuntimeException(); 81 | } 82 | } 83 | }); 84 | } 85 | 86 | } 87 | 88 | 89 | } 90 | --------------------------------------------------------------------------------