亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot?Webflux創(chuàng)建TCP/UDP?server并使用handler解析數(shù)據(jù)

 更新時間:2022年02月24日 09:55:10   作者:peng?chao  
這篇文章主要介紹了SpringBoot?Webflux創(chuàng)建TCP/UDP?server并使用handler解析數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

1.pom依賴

引用spring-boot-starter-webflux依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webflux</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

2.創(chuàng)建UDP/TCP Server

package com.example.demo;
import com.example.demo.handler.TcpDecoderHanlder;
import com.example.demo.handler.UdpDecoderHanlder;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import reactor.core.publisher.Flux;
import reactor.netty.tcp.TcpServer;
import reactor.netty.udp.UdpServer;
import java.time.Duration;
@SpringBootApplication
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
	@Bean
	CommandLineRunner serverRunner(UdpDecoderHandler udpDecoderHandler, TcpDecoderHandler tcpDecoderHandler) {
		return strings -> {
			createUdpServer(udpDecoderHandler);
			createTcpServer(tcpDecoderHandler);
		};
	}
	/**
	 * 創(chuàng)建UDP Server
	 * @param udpDecoderHanlder: 解析UDP Client上報數(shù)據(jù)handler
	 */
	private void createUdpServer(UdpDecoderHandler udpDecoderHandler) {
		UdpServer.create()
				.handle((in,out) -> {
					in.receive()
							.asByteArray()
							.subscribe();
					return Flux.never();
				})
				.port(8888)
				.doOnBound(conn -> conn.addHandlerLast("decoder",udpDecoderHandler)) //可以添加多個handler
				.bindNow(Duration.ofSeconds(30));
	}
	/**
	 * 創(chuàng)建TCP Server
	 * @param tcpDecoderHanlder: 解析TCP Client上報數(shù)據(jù)的handler
	 */
	private void createTcpServer(TcpDecoderHandler tcpDecoderHandler) {
		TcpServer.create()
				.handle((in,out) -> {
					in.receive()
							.asByteArray()
							.subscribe();
					return Flux.never();
				})
				.doOnConnection(conn ->
						conn.addHandler(tcpDecoderHandler)) //實例只寫了如何添加handler,可添加delimiter,tcp生命周期,decoder,encoder等handler
				.port(9999)
				.bindNow();
	}
}

3.數(shù)據(jù)解析handler(具體解析根據(jù)協(xié)議來)

解析UDP數(shù)據(jù)handler

package com.example.demo.handler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.MessageToMessageDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UdpDecoderHandler extends MessageToMessageDecoder<DatagramPacket>  {
    private static final Logger LOGGER = LoggerFactory.getLogger(TcpDecoderHandler.class);
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, DatagramPacket byteBuf, List<Object> list) throws Exception {
        ByteBuf byteBuf1 = byteBuf.content();
        int size = byteBuf1.readableBytes();
        byte[] data = new byte[size];
        byteBuf1.readBytes(data);
        LOGGER.info(new String(data));
    }
}

解析TCP數(shù)據(jù)handler

package com.example.demo.handler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TcpDecoderHandler extends MessageToMessageDecoder  {
    private static final Logger LOGGER = LoggerFactory.getLogger(TcpDecoderHandler.class);
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, Object o, List list){
        LOGGER.info("解析client上報數(shù)據(jù)");
    }
}

4.測試工具

推薦使用SocketTool調(diào)試TCP/UDP協(xié)議

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中由substring方法引發(fā)的內(nèi)存泄漏詳解

    Java中由substring方法引發(fā)的內(nèi)存泄漏詳解

    這篇文章主要介紹了Java中由substring方法引發(fā)的內(nèi)存泄漏詳解,涉及substring方法引發(fā)的內(nèi)存泄漏簡介,substring的作用和實現(xiàn)原理等相關(guān)內(nèi)容,具有一定借鑒價值,需要的朋友可以參考下
    2017-12-12
  • Python動態(tài)類型實現(xiàn)原理及過程解析

    Python動態(tài)類型實現(xiàn)原理及過程解析

    這篇文章主要介紹了Python動態(tài)類型實現(xiàn)原理及過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • 深入理解Java 線程池

    深入理解Java 線程池

    這篇文章主要介紹了Java 線程池的相關(guān)資料,文中講解非常細致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • java實現(xiàn)本地緩存的示例代碼

    java實現(xiàn)本地緩存的示例代碼

    在高性能服務(wù)架構(gòu)設(shè)計中,緩存是不可或缺的環(huán)節(jié),因此這篇文章主要為大家詳細介紹了java中如何實現(xiàn)本地緩存,感興趣的小伙伴可以了解一下
    2024-01-01
  • Java隊列同步器之CountDownLatch實現(xiàn)詳解

    Java隊列同步器之CountDownLatch實現(xiàn)詳解

    這篇文章主要介紹了Java隊列同步器之CountDownLatch實現(xiàn)詳解,CountDownLatch是一個同步工具類,它允許一個或多個線程一直等待,直到其他線程執(zhí)行完后再執(zhí)行,例如,應(yīng)用程序的主線程希望在負責(zé)啟動框架服務(wù)的線程已經(jīng)啟動所有框架服務(wù)之后執(zhí)行,需要的朋友可以參考下
    2023-12-12
  • java中的類為什么只能用public修飾?

    java中的類為什么只能用public修飾?

    這篇文章主要介紹了java中的類為什么只能用public修飾,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-12-12
  • java中簡單的截取分割字符串實例

    java中簡單的截取分割字符串實例

    下面小編就為大家?guī)硪黄猨ava中簡單的截取分割字符串實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Java實現(xiàn)公眾號功能、關(guān)注及消息推送實例代碼

    Java實現(xiàn)公眾號功能、關(guān)注及消息推送實例代碼

    公眾號開發(fā)近些年是一個比較熱門的方向,下面這篇文章主要給大家介紹了關(guān)于Java實現(xiàn)公眾號功能、關(guān)注及消息推送的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-11-11
  • Springboot整合hutool驗證碼的實例代碼

    Springboot整合hutool驗證碼的實例代碼

    在 Spring Boot 中,你可以將 Hutool 生成驗證碼的功能集成到 RESTful API 接口中,這篇文章主要介紹了Springboot整合hutool驗證碼,需要的朋友可以參考下
    2024-08-08
  • Maven build 命令介紹的使用詳解

    Maven build 命令介紹的使用詳解

    這篇文章主要介紹了Maven build 命令介紹的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06

最新評論