Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. public static void main(String[] args) throws Exception {
  2. final ClientHandler client= new ClientHandler();
  3. client.init();
  4. TestObj obj= new TestObj();
  5. client.send(obj);
  6. }
  7.  
  8. public class ClientHandler extends ChannelInboundHandlerAdapter {
  9. public Channel channel;
  10. /**
  11. * Creates a client-side handler.
  12. */
  13. public void init()
  14. {
  15. String host = "MY HOST";
  16. int port = 8114;
  17. EventLoopGroup group = new NioEventLoopGroup();
  18. Bootstrap b = new Bootstrap();
  19. try {
  20. b.group(group)
  21. .channel(NioSocketChannel.class)
  22. .handler(new ChannelInitializer<SocketChannel>() {
  23. @Override
  24. public void initChannel(SocketChannel ch) throws Exception {
  25. ChannelPipeline p = ch.pipeline();
  26. p.addLast(
  27. new ObjectEncoder(),
  28. new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())),
  29. new ClientHandler());
  30. }
  31. });
  32.  
  33. // Start the connection attempt.
  34. ChannelFuture f= b.connect(host, port);
  35. channel=f.awaitUninterruptibly().channel();
  36. TestObj obj= new TestObj();
  37. channel.writeAndFlush(obj);
  38. if (!f.isSuccess()) {
  39. // f.cause().printStackTrace();
  40. System.out.println("closing");
  41. group.shutdownGracefully();
  42. return;
  43. }
  44. }
  45. catch(Exception e)
  46. {
  47. System.out.println("Exception caught");
  48. e.printStackTrace();
  49. }
  50. }
  51. public ClientHandler() {
  52.  
  53. }
  54.  
  55. public void send(TestObj obj)
  56. {
  57. channel.writeAndFlush(obj);
  58. }
  59. @Override
  60. public void channelActive(ChannelHandlerContext ctx) {
  61. }
  62.  
  63. @Override
  64. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
  65. cause.printStackTrace();
  66. ctx.close();
  67. }
  68. }
  69.  
  70. EventLoopGroup bossGroup = new NioEventLoopGroup(1);
  71. EventLoopGroup workerGroup = new NioEventLoopGroup();
  72. try {
  73. ServerBootstrap b = new ServerBootstrap();
  74. b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
  75. b.group(bossGroup, workerGroup)
  76. .channel(NioServerSocketChannel.class)
  77. .handler(new LoggingHandler(LogLevel.DEBUG))
  78. .childHandler(new ChannelInitializer<SocketChannel>() {
  79. @Override
  80. public void initChannel(SocketChannel ch) throws Exception {
  81. ChannelPipeline p = ch.pipeline();
  82.  
  83. p.addLast(
  84. new ObjectEncoder(),
  85. new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())),
  86. new DiscardServerHandler());
  87. }
  88. });
  89.  
  90. // Bind and start to accept incoming connections.
  91. b.bind(port).sync().channel().closeFuture().sync();
  92. } finally {
  93. bossGroup.shutdownGracefully();
  94. workerGroup.shutdownGracefully();
  95. }
  96.  
  97. public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
  98. @Override
  99. public void channelRead(ChannelHandlerContext ctx, Object msg) {
  100. System.out.println("channelRead"+((TestObj)msg).getTestNumber());
  101. }
  102.  
  103. @Override
  104. public void channelReadComplete(ChannelHandlerContext ctx) {
  105. System.out.println("channelReadComplete");
  106. ctx.flush();
  107. }
  108.  
  109. @Override
  110. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
  111. cause.printStackTrace();
  112. ctx.close();
  113. }
  114. }
  115.  
  116. public static void main(String[] args) throws Exception {
  117. final ClientHandler client= new ClientHandler();
  118. client.init();
  119. TestObj obj= new TestObj();
  120. client.send(obj);
  121. }
  122.  
  123. public static void main(String[] args) throws Exception {
  124. final ClientHandler client= new ClientHandler();
  125. client.init();
  126. TestObj obj= new TestObj();
  127. client.send(obj);
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement