UE4UDP使用虚幻API的组播详细案例
时间: 2024-04-30 11:25:16 浏览: 167
udp组播应用例子
以下是使用虚幻引擎API进行组播的详细案例:
1. 首先,您需要创建一个新的虚幻引擎项目,并打开C++代码编辑器。
2. 在您的项目中创建一个新的Actor类,例如MyUDPActor。
3. 在MyUDPActor.h文件中,添加以下代码:
```cpp
#include "MyUDPActor.generated.h"
UCLASS()
class MYPROJECT_API AMyUDPActor : public AActor
{
GENERATED_BODY()
public:
AMyUDPActor();
virtual void BeginPlay() override;
void StartUDPReceiver(const FString& YourChosenSocketName, const FString& TheIP, const int32 ThePort);
bool StartUDPReceiver(const FString& YourChosenSocketName, const FString& TheIP, const int32 ThePort);
void StopUDPReceiver(const FString& YourChosenSocketName);
bool StopUDPReceiver(const FString& YourChosenSocketName);
bool UDPSocketSender(FString YourMessage, const FString& TheIP, const int32 ThePort);
FSockAddr RemoteAddr;
FUdpSocketReceiver* UDPReceiver = nullptr;
};
```
4. 在MyUDPActor.cpp文件中,添加以下代码:
```cpp
#include "MyUDPActor.h"
#include "Networking/Public/Networking.h"
AMyUDPActor::AMyUDPActor()
{
}
void AMyUDPActor::BeginPlay()
{
Super::BeginPlay();
FString IP = TEXT("127.0.0.1");
int32 Port = 10001;
FString SocketName = TEXT("MySocket");
StartUDPReceiver(SocketName, IP, Port);
}
bool AMyUDPActor::StartUDPReceiver(const FString& YourChosenSocketName, const FString& TheIP, const int32 ThePort)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("UDP Receiver started!"));
UDPReceiver = new FUdpSocketReceiver();
FIPv4Endpoint Endpoint(FIPv4Address(0, 0, 0, 0), ThePort);
TSharedRef<FInternetAddr> Addr = Endpoint.ToInternetAddr();
Addr->SetIp(*TheIP, true);
int32 BufferSize = 2 * 1024 * 1024; // 2MB buffer size
UDPReceiver->Init(BufferSize);
UDPReceiver->Start();
UDPReceiver->OnDataReceived().BindUObject(this, &AMyUDPActor::UDPSocketListener);
return true;
}
void AMyUDPActor::UDPSocketListener(const FArrayReaderPtr& ArrayReaderPtr, const FIPv4Endpoint& EndPt)
{
FString ReceivedData = FString(UTF8_TO_TCHAR((const char*)ArrayReaderPtr->GetData()));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, ReceivedData);
}
void AMyUDPActor::StopUDPReceiver(const FString& YourChosenSocketName)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("UDP Receiver stopped!"));
if (UDPReceiver)
{
UDPReceiver->Stop();
delete UDPReceiver;
UDPReceiver = nullptr;
}
}
bool AMyUDPActor::UDPSocketSender(FString YourMessage, const FString& TheIP, const int32 ThePort)
{
TSharedRef<FInternetAddr> Destination = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
bool bIsValid;
Destination->SetIp(*TheIP, bIsValid);
Destination->SetPort(ThePort);
if (!bIsValid)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("UDP Send: Invalid IP address %s"), *TheIP));
return false;
}
int32 BytesSent = 0;
FString Message = YourMessage + TEXT("\n");
TArray<uint8> Data = TArray<uint8>((const uint8*)TCHAR_TO_UTF8(*Message), Message.Len());
FSocket* SenderSocket = FUdpSocketBuilder(*YourChosenSocketName).AsReusable().WithBroadcast();
bool bDidSend = SenderSocket->SendTo(Data.GetData(), Data.Num(), BytesSent, *Destination);
if (bDidSend)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("UDP Send: Sent %d bytes to %s:%d"), BytesSent, *TheIP, ThePort));
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("UDP Send: Unable to send packet to %s:%d"), *TheIP, ThePort));
}
SenderSocket->Close();
delete SenderSocket;
return bDidSend;
}
```
5. 现在,您可以在其他Actor类中使用MyUDPActor的实例来发送和接收UDP数据包。例如:
```cpp
AMyUDPActor* MyUDPActor = GetWorld()->SpawnActor<AMyUDPActor>();
MyUDPActor->UDPSocketSender(TEXT("Hello World!"), TEXT("127.0.0.1"), 10001);
```
这将向IP地址为127.0.0.1,端口号为10001的地址发送“Hello World!”消息,并在MyUDPActor的屏幕上显示该消息。
阅读全文