Download

Udp receivre

Hello i’m trying to receive message under udp protocol
After reading some tutorials and documentation, the code i use is :

#include “recv_messag.h”
#include “Networking.h”
#include “Engine.h”

// Sets default values
Arecv_messag::Arecv_messag()
{
// 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;

}

// Called when the game starts or when spawned
void Arecv_messag::BeginPlay()
{
Super::BeginPlay();

}

// Called every frame
void Arecv_messag::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
TSharedRef<FInternetAddr> RemoteAddr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();

FString YourChosenSocketName = "namesocket";
FString TheIP = "127.0.0.1";
bool bIsValid;
RemoteAddr-&gt;SetIp(*TheIP, bIsValid);
RemoteAddr-&gt;SetPort(14442);

if (!bIsValid)
{
    UE_LOG(LogTemp, Log, TEXT("IP address was not valid!"));
}

RecvSocket = FUdpSocketBuilder(*YourChosenSocketName)
    .AsReusable()
    .WithBroadcast()
    ;
int32 SendSize = 2 * 1024 * 1024;
RecvSocket-&gt;SetSendBufferSize(SendSize, SendSize);
RecvSocket-&gt;SetReceiveBufferSize(SendSize, SendSize);

if (!RecvSocket-&gt;Bind(*RemoteAddr))
{
    UE_LOG(LogTemp, Log, TEXT("Socket not binding"));
}
int32  BytesRead;
uint8  Data;
if (RecvSocket-&gt;Recv(&Data, sizeof(Data), BytesRead))
{
    UE_LOG(LogTemp, Log, TEXT("Socket not binding %d

"), BytesRead);
}

}

the log is published but the bytesRead is always 0, someone can help ?

Do you ever send anything to the socket? It sounds like it’s working fine, but that there just isn’t any data to read.

Also, are you sure you bind the socket in Tick? That seems like it’d be a one-time thing in BeginPlay, and then you just read from the socket multiple times later.

hi nilamo
it’s was about binding the socket and setting up the endpoint
here the example that i used to solve the problem