当前位置: 首页 > wzjs >正文

做微信商城网站深圳市住建局造价站

做微信商城网站,深圳市住建局造价站,软件网站开发,知乎企业网站建设小伙伴们,你们好呀,我是老寇,跟我一起学习使用Vertx实现UDP-Server 实现UDP【响应式】 Vertx-Core地址 注意 UDP是无连接的传输,这意味着您与远程客户端没有建立持续的连接。 所以,您发送和接收的数据包都要包含有…

小伙伴们,你们好呀,我是老寇,跟我一起学习使用Vertx实现UDP-Server

实现UDP【响应式】

Vertx-Core地址

注意

UDP是无连接的传输,这意味着您与远程客户端没有建立持续的连接。

所以,您发送和接收的数据包都要包含有远程的地址。

除此之外,UDP不像TCP的使用那样安全, 这也就意味着不能保证发送的数据包一定会被对应的接收端(Endpoint)接收。【传输数据时不建立连接,因此可能导致数据包丢失

UDP最适合一些允许丢弃数据包的应用(如监视应用程序,视频直播)。

实现过程

查看源码

代码比较简单,懒得讲解啦
代码比较简单,懒得讲解啦
代码比较简单,懒得讲解啦
服务端
引入依赖
<dependency><groupId>io.vertx</groupId><artifactId>vertx-core</artifactId><version>5.0.0</version>
</dependency>

UdpServerProperties

/*** @author laokou*/
@Data
@Component
@ConfigurationProperties(prefix = "spring.udp-server")
public class UdpServerProperties {private String host = "0.0.0.0";private Set<Integer> ports = new HashSet<>(0);private boolean broadcast = false;private boolean loopbackModeDisabled = true;private String multicastNetworkInterface = null;private boolean ipV6 = false;}

VertxUdpServer

/*** @author laokou*/
@Slf4j
public final class VertxUdpServer extends AbstractVerticle {private volatile Flux<DatagramSocket> datagramSocket;private final UdpServerProperties udpServerProperties;private boolean isClosed = false;VertxUdpServer(Vertx vertx, UdpServerProperties udpServerProperties) {this.udpServerProperties = udpServerProperties;this.vertx = vertx;}@Overridepublic synchronized void start() {datagramSocket = Flux.fromIterable(udpServerProperties.getPorts()).map(port -> {DatagramSocket datagramSocket = vertx.createDatagramSocket(getDatagramSocketOption()).handler(packet -> log.info("【Vertx-UDP-Server】 => 收到数据包:{}", packet.data()));datagramSocket.listen(port, udpServerProperties.getHost()).onComplete(result -> {if (isClosed) {return;}if (result.succeeded()) {log.info("【Vertx-UDP-Server】 => UDP服务启动成功,端口:{}", port);}else {Throwable ex = result.cause();log.error("【Vertx-UDP-Server】 => UDP服务启动失败,错误信息:{}", ex.getMessage(), ex);}});return datagramSocket;});datagramSocket.subscribeOn(Schedulers.boundedElastic()).subscribe();}@Overridepublic synchronized void stop() {isClosed = true;datagramSocket.doOnNext(socket -> socket.close().onComplete(result -> {if (result.succeeded()) {log.info("【Vertx-UDP-Server】 => UDP服务停止成功");}else {Throwable ex = result.cause();log.error("【Vertx-UDP-Server】 => UDP服务停止失败,错误信息:{}", ex.getMessage(), ex);}})).subscribeOn(Schedulers.boundedElastic()).subscribe();}public void deploy() {// 部署服务vertx.deployVerticle(this);// 停止服务Runtime.getRuntime().addShutdownHook(new Thread(this::stop));}private DatagramSocketOptions getDatagramSocketOption() {DatagramSocketOptions datagramSocketOptions = new DatagramSocketOptions();datagramSocketOptions.setBroadcast(udpServerProperties.isBroadcast());datagramSocketOptions.setLoopbackModeDisabled(udpServerProperties.isLoopbackModeDisabled());datagramSocketOptions.setMulticastNetworkInterface(udpServerProperties.getMulticastNetworkInterface());datagramSocketOptions.setIpV6(udpServerProperties.isIpV6());return datagramSocketOptions;}}

VertxUdpServerManager

/*** @author laokou*/
public final class VertxUdpServerManager {private VertxUdpServerManager() {}public static void deploy(final Vertx vertx, final UdpServerProperties properties) {new VertxUdpServer(vertx, properties).deploy();}}
客户端【测试】
/*** @author laokou*/
@Slf4j
@SpringBootTest
@RequiredArgsConstructor
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
class UdpTest {private final Vertx vertx;@Testvoid test() throws InterruptedException {for (int i = 4880; i < 5000; i++) {DatagramSocket datagramSocket = vertx.createDatagramSocket();int finalI = i;datagramSocket.send("Hello Vertx", i, "127.0.0.1").onComplete(result -> {if (result.succeeded()) {log.info("【Vertx-UDP-Client】 => 发送成功,端口:{}", finalI);}else {Throwable ex = result.cause();log.error("【Vertx-UDP-Client】 => 发送失败,端口:{},错误信息:{}", finalI, ex.getMessage(), ex);}});Thread.sleep(2000);Assertions.assertDoesNotThrow(datagramSocket::close);}}}

这可以满足基本的协议开发,自行修改即可!!!

我是老寇,我们下次再见啦!

http://www.dtcms.com/wzjs/837705.html

相关文章:

  • 一个ip地址做多个网站黑龙江 俄语网站制作
  • 昆明网站定制开发深圳公司招聘网最新招聘信息
  • 上海哪家做网站关键词排名康复中心网站建设方案
  • 绍兴微网站建设wordpress 栏目分页
  • 萧山网站建设网站建设推进情况
  • 中国联通网站建设与维护网站内容建设的原则是什么
  • 何如做外贸网站推网唐山网站建设哪家优惠
  • 商务网站可以做哪些关键词优化一年的收费标准
  • 吉林智能网站建设企业网站整体排名大幅下降
  • 莱芜网站建设及优化全景图网站怎么做
  • 短视频制作完成网站网站原型图展示
  • python 兼职网站开发广东网络公司网站建设
  • 国内h5网站欣赏建设银行信用卡申请官方网站
  • 网站建设与网页设计专业企业产品推广运营公司
  • 最贵网站建设多少钱厦门小程序开发
  • 建设农业网站西宁网站建设多少钱
  • 东营 网站 建设周口seo推广
  • 做视频挣钱的网站宜州网站建设服务
  • 做网站赚多少钱百度网盟推广价格
  • 高端定制网站开发建站教程详解百度指数介绍
  • 做教育的网站有哪些内容吗电商和网站设计哪个好
  • 如果是创建的网站做网站费用分几块
  • 在网站里继费网站可以给pdf做笔记
  • 南昌市建设监督网站站长黑群晖做php网站
  • 网站主机和服务器的区别delphi 可做网站吗
  • 个人网页网站建设路由器上做网站
  • 成都网站只国际新闻最新消息内容
  • 关于网站开发的文献国内课题组建设常用网站
  • 外贸做网站用什么水资源监控能力建设门户网站
  • 做cpa的电影网站模板成都大型网站建设公司排名