UDP server in C++

Hello Everybody!

I’m a newbie in networking and have some problems with development of UDP server.
I have a task to send position and location of an object, and then assigning this values to an actor.

The client, which is sending values, written on python 2.7. Values (7 float) are packed into struct and sent via UDP socket.
Now I need to receive this values via UE4.

I have watched a lot of materials about sockets on the Internet and tried to make server on C++ according to them.
But I got a lot of linker and other errors.

Example of code: Bitbucket.

Can somebody explain in details, how to make C++ UDP server in UE4 and what kind of requirements/steps are needed.

Best regards, ILA2410.

P.S. Unreal version**:** 4.18.3. **Python: **2.7.
P.S.S. Sorry for very general question, but I need some help. :frowning:

Hi,

This wiki article may help: A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums

Regards

If you download a UE4 souce build (from GitHub), you get a number of programs next to UE4, including one called “Unreal Build Tool”. UBT can be used to create standalone programs (see BlankProgram and SlateApplication, or something like that). You can either create a lightweight program or use a standard UE4 build.

Then, in code, you want to use the “Socket Subsystem” framework to create platform-specific sockets, which you bind to your local host address.

How to bind:



Socket->SetBroadcast();
Socket->SetReuseAddr(false);
Socket->SetRecvErr();
Socket->SetReceiveBufferSize(BufferSize, BufferSize);
Socket->SetSendBufferSize(BufferSize, BufferSize);
Socket->SetNonBlocking();
bool bCanBindAll = true;
TSharedRef<FInternetAddr> Addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->GetLocalHostAddr(*GLog, bCanBindAll);
//Addr->SetAnyAddress(); // This would make the socket listen on all network adapters.

for (int i = 11000; i < 12000; i++)
{
    Addr>SetPort(i);
    if (Socket->Bind(*Addr))
    {
        UE_LOG(LogUWorks, Warning, TEXT("Messenger bound to: %s / %s"), *Addr->ToString(true), *Addr->ToString(true));
        break;
    }
}

How to read (preferably on tick):



TSharedRef<FInternetAddr> Addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();

if (Socket->RecvFrom(Data, sizeof(Data), DataSize, *Addr) && DataSize != 0)
{
    TArray<uint8> TempData = TArray<uint8>(Data, DataSize);
    TempData.Add(0);

    FString IP= Addr->ToString(false); // <true> would append the port.
    int32 Port = Addr->GetPort();
    FString Message = UTF8_TO_TCHAR(TempData.GetData());
}

How to send:


TSharedRef<FInternetAddr> Addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
bool bIsValid = false;
AddrLH->SetIp(TEXT("127.0.0.1"), bIsValid); // Destination IP
Addr->SetPort(7777); // Destination port

FString Message;

Socket->SendTo((uint8*)TCHAR_TO_UTF8(Message.GetCharArray().GetData()), Message.GetCharArray().Num(), DataSize, *Addr);