Download

[New Wiki] UDP Sockets, Sending / Receiving any custom data from 1 UE4 instance to another

Dear Community,

I’ve justed posted a new wiki that shows you how you can send/receive any custom data you want from 1 UE4 instance to another, anywhere in the world using UDP connection!

Wiki Link

465c97e71f555224eab45a2f098c46ae669ed85a.jpeg

Thanks for sharing this, Rama! :slight_smile:

Thanks for sharing this. Good job!

Very nice to hear from you both!

I hope everyone enjoys my wiki!

:heart:

Rama

rama, first you teach pointers, then you pull this! its like you can tell what i have been working on then you show me how!

thanks for this

Hee hee!

I am happy to have helped!

:slight_smile:

Rama

Hi Rama,I did as you said in the wiki tutorial,but I can’t get it work.
The sender worked OK but the receiver can not receive any data.
It seems that the recv function did not work.
GRW86N_A.png

Well I shared with you the code that I myself used, so you might want to verify your external program and UE4, it most be some kind of connection issue that I cannot assist with :slight_smile:

Rama

Ive got a big problem.

Your code is working great, unfortunately i can’t get it working to spawn an actor after UDP event.
My Engine always crashes because the UDP receiving is not inside main thread.
BP Event is being called, but when i try to Spawn an Actor, whether in a custom character function that is called by RamaUDPReceiver.cpp or inside the called blueprint BPEvent_DataReceived.

How can i go back to the main thread that i can work as usual ? this kills me :frowning:

Same problem here. Have you solved it?

What you can do is a run a 0.01 timer in the game thread that watches for a global bool flag that you can set inside of the Recv func, and same raw data as well like location or type of actor to spawn (FVector, String, float, etc).

As long as you dont use pointers, you can set that simple data from ::Recv, and set the bool that you want the timer in the game thread to look for and know that the other data has now been filled.

When using simple data types only, this sort of communication between game thread and the UDP socket thread will go quite well :slight_smile:

If your updates are happening ultra frequently, faster than 0.01 you might have issues but even something like every 0.1 seconds should give the game thread enough time to get the updates.

The game thread should set the bool back to false once it acts on the data so it knows not to run again till the other thread flips the bool again.

Rama

Thanks for helping. I found a solution in another guy’s post though.

According to him, it’s a thing after 4.12.5. All that’s needed is to call Start() on FUdpSocketReceiver.

Anyway this solved my problem.

Hi Rama

I can’t get it work.

got Compiler errors… pls help.

RamaUDPReceiver.h(14): error C2079: “RamaUDPReceiver_eventBPEvent_DataReceived_Parms::ReceivedData” verwendet undefiniertes struct “FAnyCustomData”
UDPSendReceive.generated.cpp(16): error C2440: “=”: “const FAnyCustomData” kann nicht in “int” konvertiert werden
UDPSendReceive.generated.cpp(16): note: Kein benutzerdefinierter Konvertierungsoperator verfügbar, der diese Konvertierung durchführen kann, oder der Operator kann nicht aufgerufen werden
RamaUDPReceiver.h(14): error C2079: “RamaUDPReceiver_eventBPEvent_DataReceived_Parms::ReceivedData” verwendet undefiniertes struct “FAnyCustomData”
RamaUDPReceiver.cpp(76): error C2079: “Data” verwendet undefiniertes struct “FAnyCustomData”
RamaUDPReceiver.cpp(80): error C2664: “void ARamaUDPReceiver::BPEvent_DataReceived(const FAnyCustomData &)” : Konvertierung von Argument 1 von “int” in “const FAnyCustomData &” nicht möglich
RamaUDPReceiver.cpp(80): note: Ursache: Konvertierung von “int” in “const FAnyCustomData” nicht möglich
RamaUDPReceiver.cpp(80): note: Der nicht definierte Typ “FAnyCustomData” wird verwendet.
RamaUDPReceiver.generated.h(11): note: Siehe Deklaration von “FAnyCustomData”
RamaUDPSender.cpp(35): error C2511: “bool ARamaUDPSender::StartUDPSender(const FString &,const FString &,const int32)”: Überladene Memberfunktion nicht in “ARamaUDPSender” gefunden
RamaUDPSender.h(15): note: Siehe Deklaration von “ARamaUDPSender”
RamaUDPSender.cpp(37): error C2597: Ungültiger Verweis auf nicht-statischen Member “ARamaUDPSender::RemoteAddr”
RamaUDPSender.cpp(40): error C2227: Links von “->SetIp” muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden.
RamaUDPSender.cpp(41): error C2227: Links von “->SetPort” muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden.
RamaUDPSender.cpp(45): error C2352: “ARamaUDPSender::ScreenMsg”: Unzulässiger Aufruf einer nicht statischen Memberfunktion
RamaUDPSender.h(51): note: Siehe Deklaration von “ARamaUDPSender::ScreenMsg”
RamaUDPSender.cpp(52): error C2597: Ungültiger Verweis auf nicht-statischen Member “ARamaUDPSender::SenderSocket”
RamaUDPSender.cpp(59): error C2227: Links von “->SetSendBufferSize” muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden.
RamaUDPSender.cpp(60): error C2227: Links von “->SetReceiveBufferSize” muss sich ein Zeiger auf Klassen-/Struktur-/Union-/generischen Typ befinden.
RamaUDPSender.cpp(80): error C2079: “NewData” verwendet undefiniertes struct “FAnyCustomData”
RamaUDPSender.cpp(81): error C2228: Links von “.Scale” müssen sich in einer Klasse/Struktur/Union befinden
RamaUDPSender.cpp(81): note: Typ ist “int”
RamaUDPSender.cpp(82): error C2228: Links von “.Count” müssen sich in einer Klasse/Struktur/Union befinden
RamaUDPSender.cpp(82): note: Typ ist “int”
RamaUDPSender.cpp(83): error C2228: Links von “.Color” müssen sich in einer Klasse/Struktur/Union befinden
RamaUDPSender.cpp(83): note: Typ ist “int”

It seems your custom struct wasn’t declared properly? Those UXXX macros can be tricky since they seem to be implemented in the generated headers. I’d suggest you declare that in an engine generated unreal class. Z.B. you add a new c++ class in engine that inherits from Actor and you cut and paste everything from USTRUCT() to the FORCEINLINE function to the new class’s header, right above the UCLASS() and beneath the #include “…generated.h”

Also make sure your “bool ARamaUDPSender::StartUDPSender(const FString &,const FString &,const int32)” function has consistent parameters in your header and source file.

In general, with those mostly helpful .generated.h, you’ll likely get a ton of errors for even a tiny typo. Just debug from the first error you get and compile after you(think you)'ve done something right.