Version: 5.5 (switch to 5.6b)
LanguageEnglish
  • C#
  • JS

Script language

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

NetworkTransport.Connect

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 function Connect(hostId: int, address: string, port: int, exeptionConnectionId: int, out error: byte): int;
public static int Connect(int hostId, string address, int port, int exeptionConnectionId, out byte error);

Parameters

hostId Host id associated with this connection (retrieved when calling NetworkTransport.AddHost).
address IPv4 address of the other peer.
port Port of the other peer.
exceptionConnectionId Set to 0 in the case of a default connection.
error Error (can be casted to NetworkError for more information).

Returns

int A unique connection identifier on success (otherwise zero).

Description

Tries to establish a connection to another peer.

The error parameter will return a non-zero value if an error occurs, this value can be casted to a NetworkError for more information.

NetworkTransport.Connect can still fail despite returning a non-zero connection id, so make sure to poll NetworkTransport.Receive or NetworkTransport.ReceiveFromHost to listen for events. If everything is OK a NetworkEventType.ConnectEvent will be returned.

#pragma strict
public class ConnectExample extends MonoBehaviour {
	var connectionId: int;
	var channelId: int;
	var hostId: int;
	function Start() {
		// Init Transport using default values.
		NetworkTransport.Init();
		// Create a connection config and add a Channel.
		var config: ConnectionConfig = new ConnectionConfig();
		channelId = config.AddChannel(QosType.Reliable);
		// Create a topology based on the connection config.
		var topology: HostTopology = new HostTopology(config, 10);
		// Create a host based on the topology we just created, and bind the socket to port 12345.
		hostId = NetworkTransport.AddHost(topology, 12345);
		// Connect to the host with IP 10.0.0.42 and port 54321
		var error: char;
		connectionId = NetworkTransport.Connect(hostId, "10.0.0.42", 54321, 0, error);
	}
	function Update() {
		var outHostId: int;
		var outConnectionId: int;
		var outChannelId: int;
		var buffer: char[] = new char[1024];
		var bufferSize: int = 1024;
		var receiveSize: int;
		var error: char;
		var evnt: NetworkEventType = NetworkTransport.Receive(outHostId, outConnectionId, outChannelId, buffer, bufferSize, receiveSize, error);
		switch (evnt) {
			case NetworkEventType.ConnectEvent:if (outHostId == hostId && outConnectionId == connectionId && NetworkErrorerror == NetworkError.Ok) {
				Debug.Log("Connected");
			}

break; case NetworkEventType.DisconnectEvent:if (outHostId == hostId && outConnectionId == connectionId) { Debug.Log("Connected, error:" + error.ToString()); }

break; } } }
using UnityEngine.Networking;

public class ConnectExample : MonoBehaviour { int connectionId; int channelId; int hostId;

void Start() { // Init Transport using default values. NetworkTransport.Init(); // Create a connection config and add a Channel. ConnectionConfig config = new ConnectionConfig(); channelId = config.AddChannel(QosType.Reliable);

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

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

// Connect to the host with IP 10.0.0.42 and port 54321 byte error; connectionId = NetworkTransport.Connect(hostId, "10.0.0.42", 54321, 0, out error); }

void Update() { int outHostId; int outConnectionId; int outChannelId; byte[] buffer = new byte[1024]; int bufferSize = 1024; int receiveSize; byte error;

NetworkEventType evnt = NetworkTransport.Receive(out outHostId, out outConnectionId, out outChannelId, buffer, bufferSize, out receiveSize, out error); switch (evnt) { case NetworkEventType.ConnectEvent: if(outHostId == hostId && outConnectionId == connectionId && (NetworkError)error == NetworkError.Ok) { Debug.Log("Connected"); } break; case NetworkEventType.DisconnectEvent: if(outHostId == hostId && outConnectionId == connectionId) { Debug.Log("Connected, error:" + error.ToString()); } break; } } }