博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
netty学习:UDP服务器与Spring整合
阅读量:4648 次
发布时间:2019-06-09

本文共 9380 字,大约阅读时间需要 31 分钟。

最近接到一个关于写UDP服务器的任务,然后去netty官网下载了netty的jar包(),解压后,可以看到上面有不少example,找到其中的关于UDP的例子。

在此学习。

直接上栗子:

 服务端:QuoteOfTheMomentServer.java(其中的代码稍微有点修改,测试了下redis,需要的同学可以直接把jar包中的栗子拷贝下来即可)

1 package com.wj.test; 2  3 import org.slf4j.Logger; 4 import org.slf4j.LoggerFactory; 5  6 import io.netty.bootstrap.Bootstrap; 7 import io.netty.channel.ChannelOption; 8 import io.netty.channel.EventLoopGroup; 9 import io.netty.channel.nio.NioEventLoopGroup;10 import io.netty.channel.socket.nio.NioDatagramChannel;11 12 /**13  * A UDP server that responds to the QOTM (quote of the moment) request to a {
@link QuoteOfTheMomentClient}.14 *15 * Inspired by the official16 * Java tutorial.17 */18 public final class QuoteOfTheMomentServer {19 20 private static final int PORT = Integer.parseInt(System.getProperty("port", "8889"));21 22 private static final Logger log=LoggerFactory.getLogger(QuoteOfTheMomentServer.class);23 24 public static void main(String[] args) throws Exception {25 EventLoopGroup group = new NioEventLoopGroup();26 log.info("QuoteOfTheMomentServer" + " + start");27 try {28 Bootstrap b = new Bootstrap();29 b.group(group)30 .channel(NioDatagramChannel.class)31 .option(ChannelOption.SO_BROADCAST, true)32 .handler(new QuoteOfTheMomentServerHandler());33 34 b.bind(PORT).sync().channel().closeFuture().await();35 } finally {36 group.shutdownGracefully();37 }38 }39 }
 
1 /* 2  * Copyright 2012 The Netty Project 3  * 4  * The Netty Project licenses this file to you under the Apache License, 5  * version 2.0 (the "License"); you may not use this file except in compliance 6  * with the License. You may obtain a copy of the License at: 7  * 8  *   http://www.apache.org/licenses/LICENSE-2.0 9  *10  * Unless required by applicable law or agreed to in writing, software11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the13  * License for the specific language governing permissions and limitations14  * under the License.15  */16 package com.wj.test;17 18 import io.netty.buffer.Unpooled;19 import io.netty.channel.ChannelHandlerContext;20 import io.netty.channel.SimpleChannelInboundHandler;21 import io.netty.channel.socket.DatagramPacket;22 import io.netty.util.CharsetUtil;23 import redis.clients.jedis.Jedis;24 25 import java.util.Random;26 import org.slf4j.Logger;27 import org.slf4j.LoggerFactory;28 29 import com.wj.test.redis.RedisUtil;30 31 32 33 public class QuoteOfTheMomentServerHandler extends SimpleChannelInboundHandler
{34 35 private static final Logger log=LoggerFactory.getLogger(QuoteOfTheMomentServerHandler.class);36 37 private static final Random random = new Random();38 39 // Quotes from Mohandas K. Gandhi:40 private static final String[] quotes = {41 "Where there is love there is life.",42 "First they ignore you, then they laugh at you, then they fight you, then you win.",43 "Be the change you want to see in the world.",44 "The weak can never forgive. Forgiveness is the attribute of the strong.",45 };46 47 private static String nextQuote() {48 int quoteId;49 synchronized (random) {50 quoteId = random.nextInt(quotes.length);51 }52 return quotes[quoteId];53 }54 55 @Override56 public void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {57 System.err.println(packet);58 59 60 61 if (packet.content().toString(CharsetUtil.UTF_8) != null ) {62 63 System.out.println(packet.content().toString(CharsetUtil.UTF_8));64 System.out.println(packet.sender());65 66 Jedis jedisK = RedisUtil.getJedis();67 jedisK.select(1);68 jedisK.lpush("test:udp:msg", packet.content().toString(CharsetUtil.UTF_8));69 70 71 72 ctx.write(new DatagramPacket(73 // Unpooled.copiedBuffer("QOTM: " + nextQuote(), CharsetUtil.UTF_8), packet.sender()));74 Unpooled.copiedBuffer("QOTM: " + "sometest", CharsetUtil.UTF_8), packet.sender()));75 }else {76 log.info("Exception");77 }78 }79 80 @Override81 public void channelReadComplete(ChannelHandlerContext ctx) {82 ctx.flush();83 }84 85 @Override86 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {87 cause.printStackTrace();88 // We don't close the channel because we can keep serving requests.89 }90 }

 

客户端:QuoteOfTheMomentClient.java

1 /* 2  * Copyright 2012 The Netty Project 3  * 4  * The Netty Project licenses this file to you under the Apache License, 5  * version 2.0 (the "License"); you may not use this file except in compliance 6  * with the License. You may obtain a copy of the License at: 7  * 8  *   http://www.apache.org/licenses/LICENSE-2.0 9  *10  * Unless required by applicable law or agreed to in writing, software11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the13  * License for the specific language governing permissions and limitations14  * under the License.15  */16 package com.wj.test;17 18 import io.netty.bootstrap.Bootstrap;19 import io.netty.buffer.Unpooled;20 import io.netty.channel.Channel;21 import io.netty.channel.ChannelOption;22 import io.netty.channel.EventLoopGroup;23 import io.netty.channel.nio.NioEventLoopGroup;24 import io.netty.channel.socket.DatagramPacket;25 import io.netty.channel.socket.nio.NioDatagramChannel;26 import io.netty.util.CharsetUtil;27 import io.netty.util.internal.SocketUtils;28 29 /**30  * A UDP broadcast client that asks for a quote of the moment (QOTM) to {
@link QuoteOfTheMomentServer}.31 *32 * Inspired by the official33 * Java tutorial.34 */35 public final class QuoteOfTheMomentClient {36 37 static final int PORT = Integer.parseInt(System.getProperty("port", "8889"));38 39 public static void main(String[] args) throws Exception {40 41 EventLoopGroup group = new NioEventLoopGroup();42 try {43 Bootstrap b = new Bootstrap();44 b.group(group)45 .channel(NioDatagramChannel.class)46 .option(ChannelOption.SO_BROADCAST, true)47 .handler(new QuoteOfTheMomentClientHandler());48 49 Channel ch = b.bind(0).sync().channel();50 51 // Broadcast the QOTM request to port 8080.52 String str = "38567071 13801783144 18917565379 20170621183115 20170621183118 05";53 ch.writeAndFlush(new DatagramPacket(54 Unpooled.copiedBuffer(str, CharsetUtil.UTF_8),55 SocketUtils.socketAddress("localhost", PORT))).sync();56 57 // QuoteOfTheMomentClientHandler will close the DatagramChannel when a58 // response is received. If the channel is not closed within 5 seconds,59 // print an error message and quit.60 if (!ch.closeFuture().await(5000)) {61 System.err.println("QOTM request timed out.");62 }63 } finally {64 group.shutdownGracefully();65 }66 }67 }

 

1 package com.wj.test; 2  3 /* 4  * Copyright 2012 The Netty Project 5  * 6  * The Netty Project licenses this file to you under the Apache License, 7  * version 2.0 (the "License"); you may not use this file except in compliance 8  * with the License. You may obtain a copy of the License at: 9  *10  *   http://www.apache.org/licenses/LICENSE-2.011  *12  * Unless required by applicable law or agreed to in writing, software13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the15  * License for the specific language governing permissions and limitations16  * under the License.17  */18 19 import io.netty.channel.ChannelHandlerContext;20 import io.netty.channel.SimpleChannelInboundHandler;21 import io.netty.channel.socket.DatagramPacket;22 import io.netty.util.CharsetUtil;23 24 public class QuoteOfTheMomentClientHandler extends SimpleChannelInboundHandler
{25 26 @Override27 public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {28 String response = msg.content().toString(CharsetUtil.UTF_8);29 if (response.startsWith("QOTM: ")) {30 System.out.println("++++Quote of the Moment: " + response.substring(6));31 ctx.close();32 }33 }34 35 @Override36 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {37 cause.printStackTrace();38 ctx.close();39 }40 }

 

该官方的栗子确实可以很好的实现UDP服务器的功能,甚至你可以写个jedis,与redis链接起来,但是往往项目开发中简单的结合是远远不够的。比如我们要将次UDP服务器与spring框架或者是spring boot框架结合起来,并使用RedisTemplate的操作类访问Redis或者使用JPA链接MySQL的时候,那么该server就要更改了,下一篇文章将会讲解此UDP服务器如何与spring boot框架整合起来,并使用RedisTemplate的操作类访问Redis。

 

posted on
2017-08-30 10:41 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/wj0816/p/7451918.html

你可能感兴趣的文章
USACO 1.5.4 Checker Challenge
查看>>
第二阶段站立会议7
查看>>
[18]Debian Linux Install GNU GCC Compiler and Development Environment
查看>>
JAVA多线程
查看>>
ACE(Adaptive Communication Environment)介绍
查看>>
delphi 更改DBGrid 颜色技巧
查看>>
python编码问题
查看>>
POJ 2031 Building a Space Station
查看>>
面向对象1
查看>>
编程开发之--java多线程学习总结(5)
查看>>
如何让 zend studio 10 识别 Phalcon语法并且进行语法提示
查看>>
任意阶幻方(魔方矩阵)C语言实现
查看>>
视频教程--ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
查看>>
第五次作业
查看>>
织梦教程
查看>>
杭电多校 Harvest of Apples 莫队
查看>>
java 第11次作业:你能看懂就说明你理解了——this关键字
查看>>
C/C++心得-结构体
查看>>
函数名作为参数传递
查看>>
apt-get for ubuntu 工具简介
查看>>