热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

NettyUDP通信Demo

1.服务端代码package udp;import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.

1.服务端代码

package udp;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;

public class UdpServer {

public static void main(String[] args) {try {Bootstrap b = new Bootstrap();EventLoopGroup group = new NioEventLoopGroup();b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true).handler(new UdpServerHandler());b.bind(2555).sync().channel().closeFuture().await();} catch (InterruptedException e) {e.printStackTrace();}
}private static class UdpServerHandler extends SimpleChannelInboundHandler {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {ByteBuf buf = packet.copy().content();byte[] req = new byte[buf.readableBytes()];buf.readBytes(req);String body = new String(req, "UTF-8");System.out.println(body);//打印收到的信息//向客户端发送消息String json = "来自服务端: 南无阿弥陀佛";// 由于数据报的数据是以字符数组传的形式存储的,所以传转数据byte[] bytes = json.getBytes("UTF-8");DatagramPacket data = new DatagramPacket(Unpooled.copiedBuffer(bytes), packet.sender());ctx.writeAndFlush(data);//向客户端发送消息}
}

}

2.客户端代码

package udp;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.util.CharsetUtil;

import java.net.InetSocketAddress;

public class UdpClient {

public static final int MessageReceived = 0x99;
private static int scanPort = 2555;public UdpClient(int scanPort) {this.scanPort = scanPort;
}private static class CLientHandler extends SimpleChannelInboundHandler {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {String body = packet.content().toString(CharsetUtil.UTF_8);System.out.println(body);}
}public static void main(String[] args) {EventLoopGroup group = new NioEventLoopGroup();try {Bootstrap b = new Bootstrap();b.group(group).channel(NioDatagramChannel.class).handler(new CLientHandler());Channel ch = b.bind(0).sync().channel();ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("来自客户端:南无本师释迦牟尼佛", CharsetUtil.UTF_8),new InetSocketAddress("127.0.0.1", scanPort))).sync();ch.closeFuture().await();} catch (Exception e) {e.printStackTrace();} finally {group.shutdownGracefully();}
}

}

3.分别启动

这里写图片描述

这里写图片描述


推荐阅读
author-avatar
百变精灵545
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有