cn.pengj.udpdemo.EchoSeverHandler.java Source code

Java tutorial

Introduction

Here is the source code for cn.pengj.udpdemo.EchoSeverHandler.java

Source

/*
 * Copyright (C) 2016 ?(52im.net) The MobileIMSDK Project. 
 * All rights reserved.
 * Project URL:https://github.com/JackJiang2011/MobileIMSDK
 *  
 * ?(52im.net) - ?! PROPRIETARY/CONFIDENTIAL.
 * Use is subject to license terms.
 */
package cn.pengj.udpdemo;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;

public class EchoSeverHandler extends SimpleChannelInboundHandler<DatagramPacket> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
        // ??
        ByteBuf buf = (ByteBuf) packet.copy().content();
        byte[] req = new byte[buf.readableBytes()];
        buf.readBytes(req);
        String body = new String(req, CharsetUtil.UTF_8);
        System.out.println("?NOTE>>>>>> ?" + body);

        // ???
        ctx.writeAndFlush(new DatagramPacket(
                Unpooled.copiedBuffer("HelloServer" + System.currentTimeMillis(),
                        CharsetUtil.UTF_8),
                packet.sender())).sync();
    }

    //   @Override
    //   public void channelRegistered(ChannelHandlerContext ctx) throws Exception 
    //   {
    //      super.channelRegistered(ctx);
    //      //System.out.println("I got it!");
    //   }
}