Skip to content

Instantly share code, notes, and snippets.

@herberteuler
Created November 20, 2014 06:12
Show Gist options
  • Save herberteuler/5c857f48ffcd84c6e183 to your computer and use it in GitHub Desktop.
Save herberteuler/5c857f48ffcd84c6e183 to your computer and use it in GitHub Desktop.
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.GenericFutureListener;
public class Client {
static final int BUFFER_SIZE = 1048576;
public static class MyHandler extends SimpleChannelInboundHandler<ByteBuf> {
final ByteBuffer buf = ByteBuffer.allocate(BUFFER_SIZE);
@Override
public void channelActive(ChannelHandlerContext c)
throws Exception {
System.out.println("active");
c.writeAndFlush(Unpooled.copiedBuffer("netty", CharsetUtil.UTF_8));
}
@Override
public void channelRead(ChannelHandlerContext c, Object msg)
throws Exception {
System.out.println("read");
super.channelRead(c, msg);
}
@Override
public void channelRead0(ChannelHandlerContext c, ByteBuf in)
throws Exception {
System.out.println("read0");
in.getBytes(0, buf);
}
@Override
public void channelReadComplete(ChannelHandlerContext c)
throws Exception {
System.out.println("read-complete");
buf.flip();
System.out.println("Received " + new String(buf.array()));
}
@Override
public void exceptionCaught(ChannelHandlerContext c, Throwable cause)
throws Exception {
cause.printStackTrace();
c.close();
}
@Override
public void channelInactive(ChannelHandlerContext c)
throws Exception {
System.out.println("inactive");
super.channelInactive(c);
}
@Override
public void channelRegistered(ChannelHandlerContext c)
throws Exception {
System.out.println("registered");
super.channelRegistered(c);
}
@Override
public void channelUnregistered(ChannelHandlerContext c)
throws Exception {
System.out.println("unregistered");
super.channelUnregistered(c);
}
}
public static void main(String[] args) throws Exception {
String host = args[0];
int port = Integer.parseInt(args[1]);
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch)
throws Exception {
ch.pipeline().addLast(new MyHandler());
}
});
ChannelFuture f = b.connect().sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment