1. package cn.enjoyedu.udp.unicast;
    2. import io.netty.channel.ChannelHandlerContext;
    3. import io.netty.channel.SimpleChannelInboundHandler;
    4. import io.netty.channel.socket.DatagramPacket;
    5. import io.netty.util.CharsetUtil;
    6. public class QuestoinHandler extends
    7. SimpleChannelInboundHandler<DatagramPacket> {
    8. @Override
    9. protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg)
    10. throws Exception {
    11. //获得应答,DatagramPacket提供了content()方法取得报文的实际内容
    12. String response = msg.content().toString(CharsetUtil.UTF_8);
    13. if (response.startsWith(UdpAnswerSide.ANSWER)) {
    14. System.out.println(response);
    15. ctx.close();
    16. }
    17. }
    18. @Override
    19. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
    20. throws Exception {
    21. cause.printStackTrace();
    22. ctx.close();
    23. }
    24. }