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

UE UDP通信

1.确保工程为C++工程,在项目工程的xx.Build.cs中加入Networking和Sockets模块。

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "Networking", "Sockets"});

2.C++创建Actor,命名为UDPClient。

3.编写代码UDPClient.h和UDPClient.cpp实现UDP发送和接收功能。

// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Runtime/Sockets/Public/Sockets.h"
#include "Sockets/Public/SocketSubsystem.h"
#include "Runtime/Networking/Public/Common/UdpSocketBuilder.h"
#include "Runtime/Networking/Public/Common/UdpSocketReceiver.h"
#include "Networking/Public/Interfaces/IPv4/IPv4Address.h"
#include "UDPClient.generated.h"UCLASS()
class MYBLANK_API AUDPClient : public AActor
{GENERATED_BODY()public:	// Sets default values for this actor's propertiesAUDPClient();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;public:	// Called every framevirtual void Tick(float DeltaTime) override;public:FSocket* udpSocket;//远程的地址TSharedPtr<FInternetAddr> RemoteAddr;UFUNCTION(BlueprintCallable, Category = "UDP")bool CreateUdp(const FString& socketName, const FString& targetIP, const int32 targetPort, const int32 selfPort);UFUNCTION(BlueprintCallable, Category = "UDP")bool SendMsg(FString msg);UFUNCTION(BlueprintCallable, Category = "UDP")void RecvMsg(bool& result, FString& msg);};
// Fill out your copyright notice in the Description page of Project Settings.#include "UDPClient.h"// Sets default values
AUDPClient::AUDPClient()
{// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = true;udpSocket = NULL;
}// Called when the game starts or when spawned
void AUDPClient::BeginPlay()
{Super::BeginPlay();}// Called every frame
void AUDPClient::Tick(float DeltaTime)
{Super::Tick(DeltaTime);}void AUDPClient::EndPlay(const EEndPlayReason::Type EndPlayReason)
{Super::EndPlay(EndPlayReason);if (udpSocket){udpSocket->Close();ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(udpSocket);}
}bool AUDPClient::CreateUdp(const FString& socketName, const FString& targetIP, const int32 targetPort, const int32 selfPort)
{bool bIsValid;RemoteAddr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();RemoteAddr->SetIp(*targetIP, bIsValid);RemoteAddr->SetPort(targetPort);if (!bIsValid){UE_LOG(LogTemp, Warning, TEXT("CreateUdp>> IP address was not valid! "), *targetIP);return false;}int32 BufferSize = 2 * 1024 * 1024;FIPv4Endpoint Endpoint(FIPv4Address::Any, selfPort);  //所有ip地址本地udpSocket = FUdpSocketBuilder(*socketName).AsReusable().WithBroadcast() // 广播.WithSendBufferSize(BufferSize).AsNonBlocking().BoundToEndpoint(Endpoint).WithReceiveBufferSize(BufferSize);udpSocket->SetSendBufferSize(BufferSize, BufferSize);udpSocket->SetReceiveBufferSize(BufferSize, BufferSize);return bIsValid;
}bool AUDPClient::SendMsg(FString msg)//发送消息
{if (!udpSocket){UE_LOG(LogTemp, Warning, TEXT("No udpSocket"));return false;}int32 BytesSent = 0;FString serialized = msg;TCHAR* serializedChar = serialized.GetCharArray().GetData();int32 size = FCString::Strlen(serializedChar);int32 sent = 0;udpSocket->SendTo((uint8*)TCHAR_TO_UTF8(serializedChar), size, BytesSent, *RemoteAddr);if (BytesSent < 0){const FString Str = "Socket is valid but the receiver received 0 bytes, make sure it is listening properly!";UE_LOG(LogTemp, Error, TEXT("%s"), *Str);return false;}UE_LOG(LogTemp, Warning, TEXT("SendMsg Succcess! INFO msg = %s "), *msg);return true;
}void AUDPClient::RecvMsg(bool& result,FString& msg)//接收消息
{if (!udpSocket){UE_LOG(LogTemp, Warning, TEXT("No udpSocket"));result = false;}TSharedRef<FInternetAddr> targetAddr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();TArray<uint8> ReceivedData;//定义一个接收器uint32 Size;if (udpSocket->HasPendingData(Size)){result = true;msg = "";uint8* Recv = new uint8[Size];int32 BytesRead = 0;ReceivedData.SetNumUninitialized(FMath::Min(Size, 65507u));udpSocket->RecvFrom(ReceivedData.GetData(), ReceivedData.Num(), BytesRead, *targetAddr);//创建远程接收地址char ansiiData[1024];memcpy(ansiiData, ReceivedData.GetData(), BytesRead);//拷贝数据到接收器ansiiData[BytesRead] = 0;                            //判断数据结束FString debugData = UTF8_TO_TCHAR(ansiiData);         //字符串转换msg = debugData;}else{result = false;}
}


FUdpSocketBuilder Functions介绍:

4.创建基于UDPClient的蓝图BP_MyUDPClient。

5.打开蓝图BP_MyUDPClient实现UDP实例化、发送和接收功能。

6.将蓝图BP_MyUDPClient放入场景中启动实现U3D和UE程序UDP通信。

http://www.xdnf.cn/news/1306657.html

相关文章:

  • 小白挑战一周上架元服务——装饰器
  • 【C++】缺省参数
  • Java调用bat执行python脚本
  • 基于多分类的工业异常声检测及应用
  • Redis 知识点与应用场景
  • Linux软件编程-进程(2)及线程(1)
  • AI加持下的智能路由监控:Amazon VPC Direct Connect实战指南
  • Python 数据可视化:柱状图/热力图绘制实例解析
  • mc paper 1.20.4
  • 【机器学习深度学习】生成式评测
  • 谈谈《More Effective C++》的条款30:代理类
  • 宋红康 JVM 笔记 Day02|JVM的架构模型、生命周期、发展历程
  • 命令模式C++
  • LPDDR5训练过程
  • 【模型评估中的BLEU、ROUGE、Bertscore、BERT分别什么意思?】
  • 洛谷 P2842 纸币问题 1 -普及-
  • 系统时钟配置
  • 《WINDOWS 环境下32位汇编语言程序设计》第1章 背景知识
  • ​Visual Studio 2013.5 ULTIMATE 中文版怎么安装?iso镜像详细步骤
  • 斯诺登:数据迷雾中的哨兵与棱镜裂痕的永恒回响
  • 【Python办公】Excel转json(极速版)-可自定义累加字段(如有重复KEY)
  • 疏老师-python训练营-Day46通道注意力(SE注意力)
  • w484扶贫助农系统设计与实现
  • redis-sentinel基础概念及部署
  • HarmonyOS 实战:用 @Observed + @ObjectLink 玩转多组件实时数据更新
  • ConRFT--RSS2025--中科院自动化所--2025.4.14
  • 10.0 UML的介绍以及VisualStudio中查看类图
  • 强制从不抱怨环境。
  • 电源测试系统ATECLOUD-Power,让您告别电源模块测试痛点!
  • Vue模板引用(Template Refs)全解析1