Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) throws Exception {
- final ClientHandler client= new ClientHandler();
- client.init();
- TestObj obj= new TestObj();
- client.send(obj);
- }
- public class ClientHandler extends ChannelInboundHandlerAdapter {
- public Channel channel;
- /**
- * Creates a client-side handler.
- */
- public void init()
- {
- String host = "MY HOST";
- int port = 8114;
- EventLoopGroup group = new NioEventLoopGroup();
- Bootstrap b = new Bootstrap();
- try {
- b.group(group)
- .channel(NioSocketChannel.class)
- .handler(new ChannelInitializer<SocketChannel>() {
- @Override
- public void initChannel(SocketChannel ch) throws Exception {
- ChannelPipeline p = ch.pipeline();
- p.addLast(
- new ObjectEncoder(),
- new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())),
- new ClientHandler());
- }
- });
- // Start the connection attempt.
- ChannelFuture f= b.connect(host, port);
- channel=f.awaitUninterruptibly().channel();
- TestObj obj= new TestObj();
- channel.writeAndFlush(obj);
- if (!f.isSuccess()) {
- // f.cause().printStackTrace();
- System.out.println("closing");
- group.shutdownGracefully();
- return;
- }
- }
- catch(Exception e)
- {
- System.out.println("Exception caught");
- e.printStackTrace();
- }
- }
- public ClientHandler() {
- }
- public void send(TestObj obj)
- {
- channel.writeAndFlush(obj);
- }
- @Override
- public void channelActive(ChannelHandlerContext ctx) {
- }
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
- cause.printStackTrace();
- ctx.close();
- }
- }
- EventLoopGroup bossGroup = new NioEventLoopGroup(1);
- EventLoopGroup workerGroup = new NioEventLoopGroup();
- try {
- ServerBootstrap b = new ServerBootstrap();
- b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
- b.group(bossGroup, workerGroup)
- .channel(NioServerSocketChannel.class)
- .handler(new LoggingHandler(LogLevel.DEBUG))
- .childHandler(new ChannelInitializer<SocketChannel>() {
- @Override
- public void initChannel(SocketChannel ch) throws Exception {
- ChannelPipeline p = ch.pipeline();
- p.addLast(
- new ObjectEncoder(),
- new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())),
- new DiscardServerHandler());
- }
- });
- // Bind and start to accept incoming connections.
- b.bind(port).sync().channel().closeFuture().sync();
- } finally {
- bossGroup.shutdownGracefully();
- workerGroup.shutdownGracefully();
- }
- public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) {
- System.out.println("channelRead"+((TestObj)msg).getTestNumber());
- }
- @Override
- public void channelReadComplete(ChannelHandlerContext ctx) {
- System.out.println("channelReadComplete");
- ctx.flush();
- }
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
- cause.printStackTrace();
- ctx.close();
- }
- }
- public static void main(String[] args) throws Exception {
- final ClientHandler client= new ClientHandler();
- client.init();
- TestObj obj= new TestObj();
- client.send(obj);
- }
- public static void main(String[] args) throws Exception {
- final ClientHandler client= new ClientHandler();
- client.init();
- TestObj obj= new TestObj();
- client.send(obj);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement