i use visual studio 2019 community.
and UnrealEngine ver.4.25.4
goal is receive UDP from other program
maybe 10~20time pre second
in my test, 1 per second, something like
@anonymous_user_142fbdd5,1,35.12345,127.123456,0,0,0,0,0,0,0,0,0,0
identifier(for thins udp), index of target actor, gps lat, gps lng
and then when i see print log, there are like
,35.12345,127.123456,0,0,0,0,0,0,0,0,0,
@anonymous_user_142fbdd5,1 <-this part is missing
what makes me crazy is. sometime i restart whole Epic Games Launcher, it worked about 10seconds and have same issue.
here are codes
UDPRecive.cpp
bool AUDPSend::StartUDPSender(const FString & YourChosenSocketName, const FString & TheIP, const int32
ThePort, bool UDP)
{
RemoteAddr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
bool bIsValid;
RemoteAddr->SetIp(*TheIP, bIsValid);
RemoteAddr->SetBroadcastAddress();
RemoteAddr->SetPort(ThePort);
if (!bIsValid)
{
return false;
}
SenderSocket = FUdpSocketBuilder(*YourChosenSocketName)
.AsReusable()// enables the bound address to be reused by other sockets
.WithBroadcast()
.WithSendBufferSize(2 * 1024 * 1024)
;
check(SenderSocket->GetSocketType() == SOCKTYPE_Datagram);
int32 SendSize = 2 * 1024 * 1024;
SenderSocket->SetSendBufferSize(SendSize, SendSize);
SenderSocket->SetReceiveBufferSize(SendSize, SendSize);
if (bIsValid)
{
bIsValid = true;
}
return bIsValid;
}
void AUDPRecive::DataRecv(FString & str, bool & success)
{
if (!ListenSocket)
{
ScreenMsg("No sender socket");
success = false;
}
TSharedRef<FInternetAddr> targetAddr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
TArray<uint8> ReceivedData;//Define a receiver
uint32 Size;
if (ListenSocket->HasPendingData(Size))
{
success = true;
str = "";
uint8 *Recv = new uint8[Size];
int32 BytesRead = 0;
// Adjust the array to a given number of elements. The new element will be initialized.
ReceivedData.SetNumUninitialized(FMath::Min(Size, 65507u));
ListenSocket->RecvFrom(ReceivedData.GetData(), ReceivedData.Num(), BytesRead, *targetAddr);
char ansiiData[1024];
FMemory::Memcpy(ansiiData, ReceivedData.GetData(), BytesRead);//Copy data to the receiver
ansiiData[BytesRead] = 0; //Judge the end of the data
FString debugData = ANSI_TO_TCHAR(ansiiData); //string conversion
str = debugData;
}
else
{
success = false;
}
}
void AUDPRecive::Recvd(const FArrayReaderPtr& ArrayReaderPtr, const FIPv4Endpoint& EndPt)
{
ScreenMsg("Received bytes", ArrayReaderPtr->Num());
uint8 data[512];
FMemory::Memzero(data, 512);
FMemory::Memcpy(data, ArrayReaderPtr->GetData(), ArrayReaderPtr->Num());
FString ReceivedData = ((const char*)data);
AsyncTask(ENamedThreads::GameThread, [&]() {
BPEvent_DataReceived(ReceivedData);
});
}
i tried ANSI_TO_TCHAR to UFT8_TO_TCHAR
it didn’t work.
and sometime when i tried close editer before stop UDP sender.
unreal engine crash by memory issue.
anyone help me slove this problem? now i’m thinking remove @letter.