How to set up an simple UDP-Listener

Hi,

I am currently trying to set up an UDP-Listener for my little Project i am working on…

I have a micro-controller which broadcast udp messages with a fixed ip and port.
Now i want to read these messages and print them as OnScreenDebugMessages.

My approach so far:

1. Create a Socket With the FUdpSocketBuilder:

FSocket *mySocket = FUdpSocketBuilder(TEXT("SomeDescription"))
.BoundToAddress(FIPv4Address(192, 168, x, y))
.BoundToPort(22222)
.Build();

2. Use this to feed a UdpSocketReceiver

FUdpSocketReceiver mySocketReceiver = FUdpSocketReceiver(mySocket, FTimespan(0, 1, 0), TEXT("SomeOtherDescription"));

3. Use Unreal’s delegate system to invoke the the OnScreenDebugMessages
…not sure about yet…

unfortunately i get these linking errors for the socket build and i cannot figure out why…

error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class ISocketSubsystem * __cdecl ISocketSubsystem::Get(class FName const &)" 
error LNK2001: unresolved external symbol "__declspec(dllimport) public: static class FIPv4Address const FIPv4Address::Any" 

Maybe somebody can tell my why i get these errors and even more important, if i am on the right track.

Thank you

In PROJECTNAME.Build.cs you need to add “Sockets” to PublicDependencyModuleNames

It should end up looking something like this.

PublicDependencyModuleNames.AddRange(new string[] { “Core”, “CoreUObject”, “Engine”, “InputCore”, “Sockets” });

Thank you very much, I think i slowly get a hang of how things actually compile with UE4… I never laid hands on such a big project with code-generation before…

Networking also has to be included for the FIPv4Address…

Can you explain the whole process of creating the client from scratch?
Thanks :slight_smile:

By this, I mean that how can we create a class that creates the socket, how can we create the function to receive the data, and how can we access that in the blueprints?

Thanks. Help would be greatly appreciated.

Sure no problem but first some question to know how detailed i should explain it… For my thesis i’m currently writing a BlueprintClass which receives CAN-Frames(Automotive Networking Protocol) packed into UDPs over Wireless Lan…

  1. Do you know how to use the unreal header tool to expose custom c++ classes to Blueprint? (by that i mean the whole UCLASS UFUNCTION U PROPERTY stuff including events)
  2. How to use Multithreading?
  3. How to use shared-pointer and shared-ref for garbage collection?

let me know and i will write you a little tutorial on what i did :wink:

I know how to expose c++ classes to blueprints. I know multithreading and shared pointers for c++ in general. What I am trying to do is to create a client that can receive the broadcast messages from another software, over the UDP protocol.

Other than this, it would be awesome if you could tell me a way to create a server that can broadcast messages over UDP protocol.

Thanks in advance.

If you could provide your listener class in c++, it would be amazing.

I’ve just written a very quick tutorial on how we’ve gone about doing this. I’ll post it to the wiki as well.

http://www.osnapgames.com/2014/05/24/connecting-to-a-third-party-socket-server-in-unreal-engine-4/

Hello Overlawled,
Could you specify on the receiving functions, especially the "HasPendingData " one since it takes a “PendingDataSize” which I have no idea how to Get this variable. Could you also explain briefly how the receiver works? (since it might listen constantly to the server right?) do we use a loop or while(1) for that? Thank you very very much for your tutorial it helps tremendously!!!

Best,
Dani.

Hey Dani,

PendingDataSize is a uint32 that gets passed into HasPendingData. HasPendingData then sets PendingDataSize to the value of the PendingData that the socket has waiting to be received.

uint32 PendingData;

Socket->HasPendingData(PendingData);

To answer your second question you are indeed correct that it happens in a loop but more specifically in a Tick or TickComponent. In my case I’ve made a component that can be added to any AActor and inside the TickComponent of the class I check whether the socket has pending data or not.

uint32 PendingData;

Socket->HasPendingData(PendingData);

if (PendingData > 0)
{
	uint8 *Recv = new uint8[PendingData];
	int32 BytesRead;

	Socket->Recv(Recv, PendingData, BytesRead);
	//Do something with the received data
}

Hope this clears it up. I’ll have to do a part 2 of the tutorial.