UDP Socket programming

I am trying to create a UDP socket that can receive data from another software. By this, I mean that how can we create a class that creates the socket, how can we create a function that establishes the connection, 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.

UE4 does have a “Sockets” module, which includes wrapper classes for BSD-style sockets and IP addresses. You’ll need to get the socket subsystem first, with a call to ISocketSubsystem::Get(), then you can create a UDP socket with ISocketSubsystem::CreateSocket with a SocketType parameter of NAME_DGram. From there, most of the functions on the FSocket class closely resemble their BSD socket API counterparts. You can find some specific examples of how we use sockets in the engine source code, take a look at IpNetDriver.cpp, for example. There you can see how to bind the socket to a port (in UIpNetDriver::InitBase and receive data on it (in UIpNetDriver::TickDispatch).

These low-level APIs are not exposed to blueprints, but I’ll let another dev chime in on how you might be able to expose your data.

Hi rleiker,

How you expose your received data to Blueprints depends on what kind of system you’re building. If you’ve got some persistent state then it probably makes sense to create a UObject subclass that manages the state, and has some blueprint callable or pure functions to access it, and then provide a way to get (or create) that object via a Blueprint function library.

If you instead want to create a one-shot async request/response system to be called from Blueprints, I’d suggest checking out UK2Node_LatentOnlineCall + UOnlineBlueprintCallProxyBase in the master branch (they’re new in 4.2). This is how we exposed things like leaderboard and achievement writes (see UAchievementWriteCallbackProxy for an example of usage).

Cheers,
Michael Noland