Obtain local ip address

Hi Guys,

I have a problem obtaining my local ipaddress.
Maybe someone could help, it should be quite straight forward :S.

These lines should give me the ip:

TSharedRef<FInternetAddr> LocalAddr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->GetLocalBindAddr(*GLog);

FString myLocalIp = LocalAddr->ToString(0);

But i always obtain a ipaddress of in the string.
The same problem i have with the RecvFrom function wich contains a pointer to the remote address.
It also returns 0’s all the time :frowning:

Any help would be appreciated.
Thanks in advance.




I’m using GetLocalHostAddr for the local IP (the IP that is used to communicate with your router).

here is some sample code:

bool canBind = false;
TSharedRef<FInternetAddr> localIp = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->GetLocalHostAddr(*GLog, canBind);

if (localIp->IsValid())
    GLog->Log(localIp->ToString(false)); // if you want to append the port (true) or not (false).

How do I set up the includes for this to work with a “launcher” version of Unreal Engine? I get compilation errors with just #include “SocketSubsystem.h”, so I guess I need to setup dependency modules, but I don’t understand how.

It’s been a while since I’ve done this and our networking has changed since then so this might be incorrect… But I think the header file is just “Networking.h” that is a header that includes a bunch of networking headers (sockets, subsystems, ipv4/6, etc). Then in your project’s Build.cs you will need to add the “Networking” module to your “PublicDependencyModuleNames” list.

1 Like

Works like a charm, thanks! You should upgrade this to an Answer — except I should point out that this always obtains if you run it on Android, which isn’t useful. But it works great on Windows.

1 Like

This worked for me:

#include <SocketSubsystem.h>
#include <IPAddress.h>

bool canBind = false;
TSharedRef<FInternetAddr> localIp = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->GetLocalHostAddr(*GLog, canBind);
return (localIp->IsValid() ? localIp->ToString(false) : "");

And in file ProjectName.Build.cs:

PublicDependencyModuleNames.AddRange(new string[] { /*Some more stuff*/ "Sockets" });

Thank you! Works great on Windows. Just a warning for others if you’re developing for Android. It has some weird issue that makes it return no matter what, though. I hope they fix it in the near future. I think it’s related to Issue UE-62267

I really need this to work in Android on WiFi :frowning:

What about iOS? Does it work there?

Thank you. Seems to be working now on Android too.

1 Like

For those still stuck isn’t working for some reason

the following have worked for me

	FIPv4Address Addr;
	FIPv4Address::Parse(localIp->ToString(false), Addr);
1 Like