Announcement

Collapse
No announcement yet.

Expose to BP: get server port and IP address

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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


    PHP Code:
    UCLASS()
    class 
    TWITCH_WWII_API UGetServerIPAndPort : public UBlueprintFunctionLibrary
    {
        
    GENERATED_BODY()
        
    UFUNCTION(BlueprintPuremeta = (DisplayName "Get Server IP Address"CompactNodeTitle "GetServerIP"Keywords "Server IP Address"), Category Game)
        static 
    FString ServerIPAndPort();
    }; 
    And now this part bothers me

    GetServerIPAndPort.cpp

    PHP Code:
    #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:
    Click image for larger version

Name:	bp.jpg
Views:	133
Size:	22.8 KB
ID:	1499908

    #2
    Ok, I tried this:

    Code:
    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?

    Comment


      #3
      Hello Mr. Wood,

      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?

      Code:
      *.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.
      GOD of DREAMs: A Unique FTPS/RPG Sandbox MMO

      Comment


        #4
        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:

        Code:
        *.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 "";
        }

        Comment


          #5
          Originally posted by GarnerP57 View Post
          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:

          Code:
          *.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.

          getiportclient.PNG
          GOD of DREAMs: A Unique FTPS/RPG Sandbox MMO

          Comment


            #6
            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.

            Comment


              #7
              Originally posted by GarnerP57 View Post
              As far as I know servers normally give their IP to another Master Server that the Clients can get a Server IP from.
              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...

              Click image for larger version  Name:	getiportserver.PNG Views:	0 Size:	26.3 KB ID:	1682711

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

              Code:
              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. https://github.com/alexhajdu/WhatsMyPublicIP-ue4-plugin
              3. https://forums.unrealengine.com/deve...168#post280168
              4. https://youtu.be/zlSa5npOyVg?list=LL...z68DMrEGHEvjzQ
              Last edited by TechLord; 11-06-2019, 04:46 AM.
              GOD of DREAMs: A Unique FTPS/RPG Sandbox MMO

              Comment


                #8
                Getting Closer....

                Code:
                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";
                }
                Click image for larger version

Name:	getlocalipport.PNG
Views:	56
Size:	113.6 KB
ID:	1684158
                GOD of DREAMs: A Unique FTPS/RPG Sandbox MMO

                Comment


                  #9
                  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.
                  Last edited by GarnerP57; 11-08-2019, 01:39 PM.

                  Comment


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

                    Comment


                      #11
                      Originally posted by GarnerP57 View Post
                      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.
                      GOD of DREAMs: A Unique FTPS/RPG Sandbox MMO

                      Comment

                      Working...
                      X