pastebin - collaborative debugging tool
kpaste.net RSS


t
Posted by Anonymous on Sat 15th Oct 2022 03:39
raw | new post

  1. package top.dsbbs2.nt;
  2.  
  3. import io.netty.bootstrap.ServerBootstrap;
  4. import io.netty.buffer.ByteBuf;
  5. import io.netty.channel.*;
  6. import io.netty.channel.nio.NioEventLoopGroup;
  7. import io.netty.channel.socket.nio.NioServerSocketChannel;
  8. import io.netty.handler.codec.string.StringDecoder;
  9. import io.netty.handler.codec.string.StringEncoder;
  10. import io.netty.util.AttributeKey;
  11.  
  12. import java.nio.charset.StandardCharsets;
  13. import java.util.Timer;
  14. import java.util.TimerTask;
  15.  
  16. public class ServerMain {
  17.     public static void main(String[] args)
  18.     {
  19.         ServerBootstrap b=new ServerBootstrap();
  20.         b.group(new NioEventLoopGroup(8),new NioEventLoopGroup(8));
  21.         b.childOption(ChannelOption.TCP_NODELAY,true);
  22.         b.channel(NioServerSocketChannel.class);
  23.         b.childHandler(new ChannelInitializer<Channel>() {
  24.             @Override
  25.             protected void initChannel(Channel ch) throws Exception {
  26.                 ch.pipeline().addLast(new StringEncoder());
  27.                 ch.pipeline().addLast(new StringDecoder());
  28.                 ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
  29.                     @Override
  30.                     public void channelActive(ChannelHandlerContext ctx) throws Exception {
  31.                         new Timer().scheduleAtFixedRate(new TimerTask() {
  32.                             @Override
  33.                             public void run() {
  34.                                 if(!ctx.channel().isActive())
  35.                                     return;
  36.                                 ctx.channel().writeAndFlush("k5RTD6n3zfiCww2dKBhkfvhkPlH4flkxuHfxTUT6GQxRxBmevKcyGFMGbdOr6olJDYczc0lAbUhtQbODc7ecoTg17tXS6Rs6AtgL1ZwhhXPjpC6K0T3ssjP7zeTNdo3lDwJHkX7yIzYRJsSJOQi32ypOHkdB0hqVITJB4JmpO9e6IMIZDmzcLtrW3VhxfgURzmlD3DML0NMVC3XewQbwAMhqUN6dNKdMxbfF4kC4D8BjEDrT4I668M8jutZLolrJdZXABxhFILI24BBKOysql5FfaJTOXP7ay9tSY5BYPyCQKslEPy3EbCNjs2m5evnCwEhFCMO7qoAcfyGx63tN4RkDsvCKhzVcnZAX6VWUHV5ba7syDXxrQYgqqjXHn0gSnR1NSzCnpH02oh5XQX5d9altnHdhm1xLJAgsq97vr7IM1kw203qYDFbeKzmh3bln45G5JYadEuttltV9RdyiSfx7mx81KQJDAKtO5PvzEUl7UndYAFQ2zw1Y4xY95qGu"+"\r\n");
  37.                             }
  38.                         }, 0, 1);
  39.                         super.channelActive(ctx);
  40.                     }
  41.                 });
  42.  
  43.             }
  44.         });
  45.         b.bind(2333);
  46.     }
  47. }
  48. package top.dsbbs2.nt;
  49.  
  50. import io.netty.bootstrap.Bootstrap;
  51. import io.netty.channel.*;
  52. import io.netty.channel.nio.NioEventLoopGroup;
  53. import io.netty.channel.socket.nio.NioSocketChannel;
  54. import io.netty.handler.codec.string.StringDecoder;
  55. import io.netty.handler.codec.string.StringEncoder;
  56.  
  57. public class ClientMain {
  58.     public static void main(String[] args)
  59.     {
  60.         Bootstrap b=new Bootstrap();
  61.         b.group(new NioEventLoopGroup(8));
  62.         b.option(ChannelOption.TCP_NODELAY,true);
  63.         b.channel(NioSocketChannel.class);
  64.         b.handler(new ChannelInitializer<Channel>() {
  65.  
  66.             @Override
  67.             protected void initChannel(Channel ch) throws Exception {
  68.                 ch.pipeline().addLast(new StringEncoder());
  69.                 ch.pipeline().addLast(new StringDecoder());
  70.                 ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
  71.                     @Override
  72.                     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  73.                         System.out.println(msg);
  74.                         super.channelRead(ctx, msg);
  75.                     }
  76.                 });
  77.             }
  78.         });
  79.         b.connect("127.0.0.1",2333);
  80.     }
  81. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at