I am trying to make a UDP server using unreal API . I am able to compile my code without crashes. but when it comes to receiving packets from a client(which i have made using python) it does not receive anything.
This is the .h file
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "UDPConnection.generated.h"
UCLASS()
class QUIZGAME_API AUDPConnection : public AActor
{
GENERATED_BODY()
public:
AUDPConnection();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
private:
FSocket* Socket;
//FInternetAddr* client;
UPROPERTY(EditAnywhere)
int32 PortNumber;
};
This is the .cpp file
// Fill out your copyright notice in the Description page of Project Settings.
#include "UDPConnection.h"
#include "Sockets.h"
#include "SocketSubsystem.h"
#include "Interfaces/IPv4/IPv4Address.h"
#include "Common/UdpSocketBuilder.h"
#include "Containers/UnrealString.h"
// Sets default values
AUDPConnection::AUDPConnection()
{
// 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;
PortNumber = 4000;
}
// Called when the game starts or when spawned
void AUDPConnection::BeginPlay()
{
Super::BeginPlay();
/*Socket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_DGram, TEXT("default"));
bool bcanBindAll;
TSharedPtr<FInternetAddr> Addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->GetLocalHostAddr(*GLog, bcanBindAll);
Addr->SetPort(7787);
Socket->Bind(*Addr);*/
bool bcanBindAll;
TSharedPtr<FInternetAddr> Addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->GetLocalHostAddr(*GLog, bcanBindAll);
FIPv4Address HostAddress;
FIPv4Address::Parse(Addr->ToString(false),HostAddress);
FIPv4Endpoint EndPoint_Server(HostAddress, PortNumber);
Socket = FUdpSocketBuilder(TEXT("UDPServerSocket"))
.BoundToEndpoint(EndPoint_Server)
.AsNonBlocking()
.AsReusable()
.WithBroadcast()
.WithReceiveBufferSize(1024);
if (Socket)
{
UE_LOG(LogTemp, Warning, TEXT("UDP Socket created"));
}
}
// Called every frame
void AUDPConnection::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
TSharedRef<FInternetAddr> Addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
TArray<uint8> RecvData;
int32 BytesRead;
if (Socket->RecvFrom(RecvData.GetData(), RecvData.Num(), BytesRead, *Addr) && BytesRead != 0)
{
//TArray<uint8> tempData = TArray<uint8>(Data, BytesRead);
//tempData.Add(0);
/*FString IP = Addr->ToString(false);
int32 port = Addr->GetPort();*/
FString Message = UTF8_TO_TCHAR(RecvData.GetData());
if (Message.Len() > 0)
{
UE_LOG(LogTemp, Warning, TEXT("Message has data %d"),BytesRead);
}
UE_LOG(LogTemp,Warning,TEXT("Message: %s"),*Message);
}
}