Version: 2018.1
public static int GetHostPort (int hostId);

Parameters

hostIdHost ID.

Returns

int The UDP port number, or -1 if an error occurred.

Description

Returns the port number assigned to the host.

NOTE: this function is not supported for Xbox One and web sockets.

using UnityEngine;
using UnityEngine.Networking;

public class ExampleScript : MonoBehaviour { int channelId; int hostId; void Start() { // Init Transport using default values. NetworkTransport.Init();

// Create a connection_config and add a Channel. ConnectionConfig connection_config = new ConnectionConfig(); channelId = connection_config.AddChannel(QosType.Reliable);

// Create a topology based on the connection config. HostTopology topology = new HostTopology(connection_config, 10);

// Create a host based on the topology we just created, and bind the socket to port assigned buy OS. hostId = NetworkTransport.AddHost(topology, 0);

int assignedPort = NetworkTransport.GetHostPort(hostId); if (assignedPort == -1) { Debug.Log("GetHostPort returns error"); } } }