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 0.0.0.0 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.

Cheers.

B

Hi,

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).
}
1 Like

Hi!
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.

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

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" });
1 Like

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 127.0.0.1 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.