网状的学习教程之网状的与编组结合发送对象

  

  

之前的一篇文章是网状的简单的学习,我们可以传递一个字符串,那么如果我们想要在网状的中传递一个对象该怎么办呢?

  

那么这个时候我们可以结合编组来传递。
  

  

  

首先需要导入两个编组的依赖包
  

        jboss-marshalling-1.3.0.CR9.jar   jboss-marshalling-serial-1.3.0.CR9.jar      

我开始学习的时候只导入了第一个jar包,没有导入第二个,结果是不报的错,但是客户端和服务端之间传递不了消息。所以两个包一定要都导入才行。
  

  

<强> MarshallingCodeCFactory工具类

        公开课MarshallingCodeCFactory {      公共静态MarshallingDecoder buildMarshallingDecoder () {   最后MarshallerFactory工厂=Marshalling.getProvidedMarshallerFactory(“串行”);   最后MarshallingConfiguration配置=new MarshallingConfiguration ();   configuration.setVersion (5);   UnmarshallerProvider提供者=new DefaultUnmarshallerProvider(工厂、配置);   MarshallingDecoder解码器=new MarshallingDecoder(供应商,1024 * 1024);   返回译码器;   }      公共静态MarshallingEncoder buildMarshallingEncoder () {   最后MarshallerFactory工厂=Marshalling.getProvidedMarshallerFactory(“串行”);   最后MarshallingConfiguration配置=new MarshallingConfiguration ();   configuration.setVersion (5);   MarshallerProvider提供者=new DefaultMarshallerProvider(工厂、配置);   MarshallingEncoder编码器=new MarshallingEncoder(供应商);   返回编码器;   }   }      

<强>服务器端

        公共类服务器{      公共静态void main (String [] args)抛出InterruptedException {//1 .第一个线程组是用于接收客户端连接的   EventLoopGroup bossGroup=new NioEventLoopGroup ();//2 .第二个线程组是用于实际的业务处理的   EventLoopGroup workerGroup=new NioEventLoopGroup ();   ServerBootstrap b=new ServerBootstrap ();   b。集团(bossGroup workerGroup);//绑定两个线程池   b.channel (NioServerSocketChannel.class);//指定NIO的模式,如果是客户端就是NioSocketChannel   b.option (ChannelOption。SO_BACKLOG, 1024);//TCP的缓冲区设置   b.option (ChannelOption。SO_SNDBUF 32 * 1024);//设置发送缓冲的大小   b.option (ChannelOption。SO_RCVBUF 32 * 1024);//设置接收缓冲区大小   b.option (ChannelOption。SO_KEEPALIVE,真);//保持连续   b。childHandler(新的ChannelInitializer () {   保护无效initChannel (SocketChannel ch){抛出异常//设置编组的编码和解码   ch.pipeline () .addLast (MarshallingCodeCFactory.buildMarshallingDecoder ());   ch.pipeline () .addLast (MarshallingCodeCFactory.buildMarshallingEncoder ());   ch.pipeline ()。addLast(新ServertHandler ());   }   });   ChannelFuture未来=b.bind (8765) .sync();//绑定端口   .sync .closeFuture future.channel()()();//等待关闭(程序阻塞在这里等待客户端请求)   bossGroup.shutdownGracefully();//关闭线程   workerGroup.shutdownGracefully();//关闭线程   }      }      

<强> ServerHandler处理类

        公开课ServertHandler延伸ChannelHandlerAdapter {      @Override   公共空间exceptionCaught (ChannelHandlerContext ctx, Throwable引起)   抛出异常{   cause.printStackTrace ();   }      @Override   公共空间channelRead (ChannelHandlerContext ctx、对象味精)   抛出异常{   发送发送=(发送)味精;   System.out.println(“客户发送:“+发送);      收到收到=new接收();   receive.setId (send.getId ());   receive.setMessage (send.getMessage ());   receive.setName (send.getName ());   ctx.writeAndFlush(接收);   }      }      

由于我们已经在服务器端和客户机端定义了传递的类型又编组工厂处理,所以此时我们接收的时候直接转成发送的对象类型就行了。
  

  

<强>客户端

        公共类客户{      公共静态void main (String [] args)抛出InterruptedException {   EventLoopGroup工人=new NioEventLoopGroup ();   引导b=new引导();   b.group(工人)   .channel (NioSocketChannel.class)   .handler(新的ChannelInitializer () {   @Override   保护无效initChannel (SocketChannel sc){抛出异常//ByteBuf buf=Unpooled.copiedBuffer (“$ _”.getBytes ());//sc.pipeline ()。addLast(新DelimiterBasedFrameDecoder(1024年,buf));//sc.pipeline ()。addLast(新StringDecoder ());   sc.pipeline () .addLast (MarshallingCodeCFactory.buildMarshallingDecoder ());   sc.pipeline () .addLast (MarshallingCodeCFactory.buildMarshallingEncoder ());   sc.pipeline ()。addLast(新ClientHandler ());   }   });   ChannelFuture f=b.connect (127.0.0.1, 8765) .sync ();   for (int i=1; i<=5;我+ +){   发送发送=new发送();   send.setId(我);   send.setMessage(“消息”+ i);   send.setName(“名称”+ i);   f.channel () .writeAndFlush(发送);   }   .sync .closeFuture f.channel () () ();   worker.shutdownGracefully ();   }   }

网状的学习教程之网状的与编组结合发送对象