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:	1208
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.
      HeadlessStudios.com - S P A G H E T T I: Ninja Swords, Guns & Monsters & Other S**t! - Join Our Discord

      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
          HeadlessStudios.com - S P A G H E T T I: Ninja Swords, Guns & Monsters & Other S**t! - Join Our Discord

          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.
              HeadlessStudios.com - S P A G H E T T I: Ninja Swords, Guns & Monsters & Other S**t! - Join Our Discord

              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:	1111
Size:	113.6 KB
ID:	1684158
                HeadlessStudios.com - S P A G H E T T I: Ninja Swords, Guns & Monsters & Other S**t! - Join Our Discord

                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.
                      HeadlessStudios.com - S P A G H E T T I: Ninja Swords, Guns & Monsters & Other S**t! - Join Our Discord

                      Comment


                        #12
                        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!

                        Comment


                          #13
                          Originally posted by e3pojedi View Post
                          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 https://github.com/alexhajdu/WhatsMyPublicIP-ue4-plugin

                          ....
                          Test #1: Server|Client Registration (support Server|Client Presence & Matchmaking) Test #2: Multiple Dedicated Server Instance Registration.
                          HeadlessStudios.com - S P A G H E T T I: Ninja Swords, Guns & Monsters & Other S**t! - Join Our Discord

                          Comment


                            #14
                            How did you get the plugin to compile? I’m having issues packaging it -
                            Code:
                            `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 '<global namespace>' already contains a definition for 'WhatsMyPublicIP' `

                            Comment


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

                              Comment

                              Working...
                              X