NettyClient.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import io.netty.bootstrap.Bootstrap;
  2. import io.netty.channel.*;
  3. import io.netty.channel.nio.NioEventLoopGroup;
  4. import io.netty.channel.socket.nio.NioSocketChannel;
  5. import io.netty.handler.codec.string.StringDecoder;
  6. import io.netty.handler.codec.string.StringEncoder;
  7. /**
  8. * 测试客户端
  9. *
  10. * @author 修唯xiuwei
  11. * @create 2018-02-05 15:56
  12. * @Email 524710549@qq.com
  13. **/
  14. public class NettyClient {
  15. public static void main(String[] args) {
  16. EventLoopGroup group = new NioEventLoopGroup();
  17. try {
  18. Bootstrap sb = new Bootstrap();
  19. // 绑定线程池
  20. sb.group(group)
  21. // 指定使用的channel
  22. .channel(NioSocketChannel.class)
  23. // 绑定监听端口
  24. // 绑定客户端连接时候触发操作
  25. .handler(new ChannelInitializer<NioSocketChannel>() {
  26. @Override
  27. protected void initChannel(NioSocketChannel ch) throws Exception {
  28. ch.pipeline().addLast(new StringDecoder());
  29. //字符串编码器
  30. ch.pipeline().addLast(new StringEncoder());
  31. //处理类
  32. ch.pipeline().addLast(new ClientHandler4());
  33. }
  34. });
  35. // 服务器异步创建绑定
  36. ChannelFuture cf = sb.bind("127.0.0.1", 2404);
  37. System.out.println("1" + cf.isSuccess());
  38. cf.sync();
  39. System.out.println("2" + cf.isSuccess());
  40. System.out.println("4" + cf.isDone());
  41. System.out.println(cf.getClass().getSimpleName());
  42. // 关闭服务器通道
  43. ChannelFuture ff = cf.channel().closeFuture();
  44. System.out.println(ff.getClass().getSimpleName());
  45. System.out.println(ff.sync());
  46. System.out.println("is==" + (cf == ff));
  47. System.out.println(cf.isDone());
  48. System.out.println("3" + cf.isSuccess());
  49. } catch (InterruptedException e) {
  50. e.printStackTrace();
  51. } finally {
  52. try {
  53. // 释放线程池资源
  54. group.shutdownGracefully().sync();
  55. } catch (InterruptedException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. //worker负责读写数据
  60. }
  61. }
  62. class ClientHandler4 extends SimpleChannelInboundHandler<String> {
  63. //接受服务端发来的消息
  64. @Override
  65. protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
  66. System.out.println("server response : " + msg);
  67. }
  68. //与服务器建立连接
  69. @Override
  70. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  71. //给服务器发消息
  72. ctx.channel().writeAndFlush("i am client !\n");
  73. System.out.println("channelActive");
  74. }
  75. //与服务器断开连接
  76. @Override
  77. public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  78. System.out.println("channelInactive");
  79. }
  80. //异常
  81. @Override
  82. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  83. //关闭管道
  84. ctx.channel().close();
  85. //打印异常信息
  86. cause.printStackTrace();
  87. }
  88. }