How to get server IP after connection to session on client?

I need to send large data chunks from server to clients using sockets.

I connect to server using Online Subsystem (using blueprint nodes Create Session, Find Sessions and Join Session). I connect through LAN and it works.

To receive client IP, I just overrided PreLogin and PostLogin methods of GameModeBase but to connect using sockets I need to know IP of server on clients. I think it is keeped somewhere but I don’t understand where.

How can I know server IP?

I succeeded!

To achieve this you need to override PlayerControllers BeginPlay

Look code below:

void AMyPlayerController::BeginPlay()
{
    if (NetConnection) // This is a field of PlayerController
    {
        GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, FString::Printf(TEXT("Net request url is %s"), *NetConnection->URL.Host));
    }
    else
    {
        GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, FString::Printf(TEXT("NetConnection is null")));
    }
}

This method runs on both client and server for each client on loading of level. On server NetConnection value is null, on client it is not and it contains IP in URL.Host.

I hope this will help somebody at future.

Can also use this, will work with steam and steam dedicated.
If its a non dedicated game you may have to append the port after it like, steam.1234557::7777

 FString URL = FString();
    
    	IOnlineSessionPtr Session = IOnlineSubsystem::Get()->GetSessionInterface();
    	if (Session)
    	{
    		FNamedOnlineSession* NamedSession = Session->GetNamedSession(GameSessionName);		
    		if(NamedSession)
    		{
    			if(IsDedicatedServerInstance()) URL.Append(TEXT("steam."));
    			URL.Append(NamedSession->SessionInfo->GetSessionId().ToString());
    		}
    	}

Thank you!
I hope it will be useful :slight_smile: