Expose to BP: get server port and IP address

I am trying to get server IP and port for current dedicated server (exposed to BP).
I successfully managed to show node in blueprints (but without function). I am new at this.
However this is code:
GetServerIPAndPort.h

UCLASS()
class TWITCH_WWII_API UGetServerIPAndPort : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()
    UFUNCTION(BlueprintPure, meta = (DisplayName = "Get Server IP Address", CompactNodeTitle = "GetServerIP", Keywords = "Server IP Address"), Category = Game)
    static FString ServerIPAndPort();
};

And now this part bothers me

GetServerIPAndPort.cpp

#include "Engine/World.h"
#include "GetServerIPAndPort.h"

FString UGetServerIPAndPort::ServerIPAndPort()
{
    //if ( GetWorld() ) return GetWorld()->URL.Port;
    return "";
}

Currently it returns empty string. But how to get server IP and port so it will look something like this:
bp.jpg

Ok, I tried this:


int32 UGetServerIPAndPort::ServerIPAndPort()
{
    UWorld* wworld = GEngine->GameViewport->GetWorld();

        if (wworld->IsServer())
        {
            return wworld->URL.Port;
        }
        return 0;

}

But UE4 crash after calling it.
Could not find anywhere something related to this.
Little help here?

1 Like

Hello [USER=ā€œ947258ā€]Mr. Wood[/USER],

I’m in the process of Exposing IP/Port to BP Function Library (C++ Plugin). Unfortunately, I hit a road block with ā€˜nonstatic member reference must be relative to a specific object’. Curious if you found a solution to this issue?



*.h
UFUNCTION(BlueprintCallable, meta = (DefaultToSelf = "WorldContextObject", DisplayName = "GetNetworkURL", Keywords = "Server Client IP Port"), Category = "Woosah")
FString GetNetworkURL();

*.cpp
FString UWoosahBPLibrary::GetNetworkURL()
{
    return GetWorld()->GetAddressURL();
}


Any assistance you can offer in this matter is greatly appreciated. Thanks in advance.

1 Like

You should make it static and use the WorldContextObject to call GetWorld since you can’t make the call directly from a static function. Optionally use BlueprintPure instead of BlueprintCallable:


*.h
   UFUNCTION(BlueprintPure, meta = (WorldContext = "WorldContextObject"))
    static const FString GetNetworkURL(UObject* WorldContextObject);

*.cpp
const FString UMyBlueprintFunctionLibrary::GetNetworkURL(UObject* WorldContextObject)
{
    if (WorldContextObject)
    {
        if (UWorld* World = WorldContextObject->GetWorld())
        {
            return World->GetAddressURL();
        }
    }
    return "";
}


Thank You @GarnerP57 . You’re an amazing knowledgeable UE4 C++ Programmer. I’m a noob, embarrassed to admit I’m new to C++. Apparently, ā€˜World->GetAddressURL()’ function only works for Clients . I’m doing more research to locate the function I need to Server Ip Port. This put me on the right path.

Thank you for the kind words, I still have a lot to learn about this engine though. I am not familiar with any function that returns the server public IP from the server itself. As far as I know servers normally give their IP to another Master Server that the Clients can get a Server IP from. The public IP is only known by the router connecting the server to the internet and anyone the router/server is sending messages to.

That is the purpose of the exposing this info to blueprints is to acquire IP & Port to send to Master Server (HTTP) for Clients. It will be used in Online Subsystem for Wordpress Plugin .

This info is dump in the Logs…

I’ve searched the UE4 code base to find the log outputs:


C:\UE4\GitHub\UnrealEngine-4.23.1\Engine\Plugins\Online\OnlineSubsystemUtils\Source\OnlineSubsystemUtils\Private\IpNetDriver.cpp(498):    UE_LOG(LogNet, Log, TEXT("%s IpNetDriver listening on port %i"), *GetDescription(), LocalURL.Port );

C:\UE4\GitHub\UnrealEngine-4.23.1\Engine\Source\Runtime\Sockets\Private\SocketSubsystem.cpp(328):            UE_LOG(LogInit, Log, TEXT("%s: I am %s (%s)"), GetSocketAPIName(), *HostName, *HostAddr->ToString(true) );

I’m reviewing the code to figure out how to use it.

Reference List

  1. https://answers.unrealengine.com/que…2236/view.html
  2. GitHub - alexhajdu/WhatsMyPublicIP-ue4-plugin: In case you need to get your public IP - this can help. C++ and Blueprints
  3. https://forums.unrealengine.com/development-discussion/blueprint-visual-scripting/4014-39-rama-s-extra-blueprint-nodes-for-you-as-a-plugin-no-c-required?p=280168#post280168
  4. https://youtu.be/zlSa5npOyVg?list=LL86N3lNsqz68DMrEGHEvjzQ

Getting Closer…



const FString UWoosahBPLibrary::GetHost(UObject* WorldContextObject)
{
    if (WorldContextObject)
    {
        if (UWorld* World = WorldContextObject->GetWorld())
        {
            bool canBind = false;
            TSharedRef<FInternetAddr> localIp = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->GetLocalHostAddr(*GLog, canBind);
            return (localIp->IsValid() ? localIp->ToString(false) : World->URL.Host);
            //return World->URL.Host;
        }
    }
    return "WorldContextObject is FALSE";
}


const FString UWoosahBPLibrary::GetPortNumber(UObject* WorldContextObject)
{
    if (WorldContextObject)
    {
        if (UWorld* World = WorldContextObject->GetWorld())
        {
            return FString::FromInt(World->URL.Port);
        }
    }
    return "WorldContextObject is FALSE";
}


getlocalipport.PNG

Getting your private network IP is simple since you just have to call the router and it will be happy to respond with your designated Private IP.
I don’t know how you would ask the router connected to the internet for the Public IP without sending a packet to an external server and it will report back with your Public IP. I guess it is possible somehow and I would also like to know how.

According to Google it can’t be done and the only solution is to use an external server which doesn’t sound like a bad solution to be honest if you pick a reliable server.

Out of interest why do you need to know the public IP on the dedicated server itself?

Developing Online Services Plugin for Wordpress. The IP Port is needed for registering to Online Services Server for Server and Client Sessions, Presence, Match Making, Tournament Management, etc. I found a solution that sends API Request via HTTP that echos back the Public IP.

@TechLord How did you get this to work? I just need to get the local IP address of the computer (192.168.xx.xx), and nothing’s working so far!

Use the code in [GitHub - alexhajdu/WhatsMyPublicIP-ue4-plugin: In case you need to get your public IP - this can help. C++ and Blueprints

…](GitHub - alexhajdu/WhatsMyPublicIP-ue4-plugin: In case you need to get your public IP - this can help. C++ and Blueprints) [TABLE=ā€œalign: centerā€]

Test #1: Server|Client Registration (support Server|Client Presence & Matchmaking)
Test #2: Multiple Dedicated Server Instance Registration.

How did you get the plugin to compile? I’m having issues packaging it -



`UATHelper: Packaging (Android (Multi:ASTC,PVRTC,DXT,ATC,ETC2,ETC1a,ETC1)):   d:\VRShooter\Plugins\WhatsMyPublicIP-ue4-plugin-master\Source\WhatsMyPublicIP\WhatsMyPublicIP.Build.cs(6,7) : error CS0101: The namespace '&lt;global namespace&gt;' already contains a definition for 'WhatsMyPublicIP' `
 

Seems like nice plugin.
I’m not sure how useful it would be in practice, especially when it comes to scalability.
I would use my own API instead of WordPress for this purpose and nothing else. And then I could always call API that will return data from the database and could be used anywhere (in Wordpress too).
However, I was working on a slightly different method. There is one master server and other servers send queries and requests via node.js or websockets (or HTTPS).
For example, a user click on Play button, it will send request to the server, server will ask master server if there are available servers to join.

  • If there are NO available servers, master server will spawn a new dedicated server instance with parameters (master server automatically handle ports and insert data into database). When dedicated server is up, it will send response that user can join and server is available.
  • If there are available servers, master server will return data about available server to user and user will join.
  • When match is over dedicated server will send info to master server and will shut down automatically.
    Also, this could be used for player invites, creating virtual lobbies etc.
    Basically, there’s nothing to write, servers will automatically spawn and shutdown on request by master server (and to do: routing users to specific server based on geoIP).
    Working on webinterface to manually spawn servers, close server, get info from servers like all players and commands for them, set max. number of players for each server, set max port range (max number of server per IP) etc.