Legacy Documentation: Version 2018.1 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

NetworkTransport.GetHostPort

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static method GetHostPort(hostId: int): int;
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.

no example available in JavaScript
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"); } } }

Did you find this page useful? Please give it a rating: