Netty的入门-UDP协议开发

User Datagram Protocal直接用IP协议进行数据发送,提供的是面向无连接,不可靠的数据投递服务。使用UDP的应用程序必须自己解决数据丢失,重复,排序,差错确认等问题。UDP数据包的格式如下:


UDP协议的特点:

1、无连接,在数据传输前,发送方和接收方互换信息使双方同步

2、UDP对接收到的数据不发确认信号

3、UDP传输数据比TCP快,系统开销也小,常用于可靠性要求不高的数据传输,如视频语音图片以及简单文件传输

public class ChineseProverbServer {

	public static void main(String[] args) {
		int port = 8089;
		EventLoopGroup group = new NioEventLoopGroup();
		try {
			Bootstrap b = new Bootstrap();
			b.group(group)
				.channel(NioDatagramChannel.class)
				.option(ChannelOption.SO_BROADCAST, true)
				.handler(new ChineseProverbServerHandler());
			b.bind(port).sync().channel().closeFuture().await();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			group.shutdownGracefully();
		}
	}
	
	static class ChineseProverbServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {

		private static final String[] DICTIONARY = {"只要功夫深铁杵磨成针", "旧时王谢堂前燕飞入寻常百姓家", "洛阳亲友如相问一片冰心在玉壶", "一寸光阴一寸金寸金难买寸光阴", "老骥伏枥志在千里"};
		
		private String nextQuote() {
			int quoteId = ThreadLocalRandom.current().nextInt(DICTIONARY.length);
			return DICTIONARY[quoteId];
		}
		
		@Override
		protected void channelRead0(ChannelHandlerContext ctx,
				DatagramPacket msg) throws Exception {
			String request = msg.content().toString(CharsetUtil.UTF_8);
			System.out.println(request);
			if ("谚语字典查询".equals(request)) {
				ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("谚语字典查询结果:" + nextQuote(), CharsetUtil.UTF_8), msg.sender()));
			}
			
		}
		
		@Override
		public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
				throws Exception {
			ctx.close();
			cause.printStackTrace();
		}
		
	}

}

public class ChineseProverbClient {

	public static void main(String[] args) {
		EventLoopGroup group = new NioEventLoopGroup();
		try {
			Bootstrap b = new Bootstrap();
			b.group(group).channel(NioDatagramChannel.class)
				.option(ChannelOption.SO_BROADCAST, true)
				.handler(new ChineseProverbClientHandlert());
			Channel ch = b.bind(0).sync().channel();
			ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("谚语字典查询", CharsetUtil.UTF_8), new InetSocketAddress("localhost", 8089)));
			if (!ch.closeFuture().await(15000)) {
				System.out.println("查询超时!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			group.shutdownGracefully();
		}
	}
	
	static class ChineseProverbClientHandlert extends SimpleChannelInboundHandler<DatagramPacket> {

		@Override
		protected void channelRead0(ChannelHandlerContext ctx,
				DatagramPacket msg) throws Exception {
			String response = msg.content().toString(CharsetUtil.UTF_8);
			if (response.startsWith("谚语字典查询结果:")) {
				System.out.println(response);
				ctx.close();
			}
		}
	}

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值