Can't join session with Steam online subsystem sometimes.

I followed this tutorial to make a multiplayer game via Steam. Everything works fine and I was able to create/join session on two different accounts/pc.
However, sometimes when I try to join a session, it failed. I tried to close firewall or use VPN, but it still fails like 50% of the time. (Create session always work with bUseLobbiesIfAvailable = true.)
Here is my DefaultEngine.ini:

[/Script/Engine.GameEngine]
+NetDriverDefinitions=(DefName="GameNetDriver",DriverClassName="OnlineSubsystemSteam.SteamNetDriver",DriverClassNameFallback="OnlineSubsystemUtils.IpNetDriver")

[OnlineSubsystem]
DefaultPlatformService=Steam

[OnlineSubsystemSteam]
bEnabled=true
SteamDevAppId=480
GameServerQueryPort=27015

[/Script/OnlineSubsystemSteam.SteamNetDriver]
NetConnectionClassName="OnlineSubsystemSteam.SteamNetConnection"

And my C++ function to create session:

void UCPP_GameInstance::CreateServer()
{
	UE_LOG(LogTemp, Warning, TEXT("CreateServer"));

	FOnlineSessionSettings SessionSettings;
	SessionSettings.bAllowJoinInProgress = true;
	SessionSettings.bIsDedicated = false;
	SessionSettings.bIsLANMatch = IOnlineSubsystem::Get()->GetSubsystemName() == "NULL";
	SessionSettings.bShouldAdvertise = true;
	SessionSettings.bUsesPresence = true;
	SessionSettings.NumPublicConnections = 5;
	SessionSettings.bUseLobbiesIfAvailable = true;

	SessionInterface->CreateSession(0, FName("My Session"), SessionSettings);
}

Find session (ignore the funciton name):

void UCPP_GameInstance::JoinServer()
{
	UE_LOG(LogTemp, Warning, TEXT("JoinServer"));

	SessionSearch = MakeShareable(new FOnlineSessionSearch());
	SessionSearch->bIsLanQuery = IOnlineSubsystem::Get()->GetSubsystemName() == "NULL";
	SessionSearch->MaxSearchResults = 150000;
	SessionSearch->QuerySettings.Set(SEARCH_PRESENCE, true, EOnlineComparisonOp::Equals);

	SessionInterface->FindSessions(0, SessionSearch.ToSharedRef());
}

Join session after session found:

void UCPP_GameInstance::OnFindSessionComplete(bool Succeeded)
{
	UE_LOG(LogTemp, Warning, TEXT("OnFindSessionComplete, Succeeded: %d"), Succeeded);
	if (Succeeded)
	{
		TArray<FOnlineSessionSearchResult> SearchResults = SessionSearch->SearchResults;

		UE_LOG(LogTemp, Warning, TEXT("SearchResults, Server Count: %d"), SearchResults.Num());

		if (SearchResults.Num())
		{
			UE_LOG(LogTemp, Warning, TEXT("Joining Server"));
			SessionInterface->JoinSession(0, "My Session", SearchResults[0]);
		}
	}
}

Client travel after join session:

void UCPP_GameInstance::OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result)
{
	UE_LOG(LogTemp, Warning, TEXT("OnJoinSessionComplete, SessionName: %s"), *SessionName.ToString());
	if (APlayerController* PController = UGameplayStatics::GetPlayerController(GetWorld(), 0))
	{
		FString JoinAddress = "";
		SessionInterface->GetResolvedConnectString(SessionName, JoinAddress);
		if (JoinAddress != "")
		{
			PController->ClientTravel(JoinAddress, ETravelType::TRAVEL_Absolute);
		}
	}
}

I know there is already a lot of posts about “Steam online subsystem not working”. But I guess none of them only fail 50% of the time? I want a clear explanation of why this happened. Maybe because of the instability of the internet? Or can I adjust the handshake times? Use TCP instead?

I am using Unreal Engine 5.3.2 on Windows 11. Running packaged build (Development) on both machine. Will be very thankful if someone can help me with this.

Here is the log when it failed.
Client: SimpleFPS.log (222.6 KB)
Listen server: SimpleFPS.log (155.0 KB)

I think the main error is this (from client when it tries to join session):

[2024.08.21-06.31.52:977][529]LogNet: Error: UEngine::BroadcastNetworkFailure: FailureType = PendingConnectionFailure, ErrorString = Your connection to the host has been lost., Driver = PendingNetDriver SteamNetDriver_2147482478
[2024.08.21-06.31.52:977][529]LogNet: Warning: Network Failure: PendingNetDriver[PendingConnectionFailure]: Your connection to the host has been lost.
[2024.08.21-06.31.52:977][529]LogNet: NetworkFailure: PendingConnectionFailure, Error: 'Your connection to the host has been lost.'

Ok I finally found the reason why it failed. It is because of the session didn’t get destroyed properly. When I close the game on the listen server, it still shows up in the search results after calling find session. Therefore, It finds 2 sessions before I try to join. And in my code it just joins the first one, which is already dead.

Guess I need to find a way to destroy the session properly or filter the search results.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.