Possible Networking.NetworkTransport errors.
A group of possible NetworkTransport errors. If an error is returned from a NetworkTransport function, you can use NetworkError to find out what type of error occured. Make sure to use NetworkTransport.Receive or similar to listen for these type of events.
//Create a Button (Create>UI>Button) for the host.
//Attach this Button in the Inspector of your GameObject.
//In Play Mode, click the Button to connect. If the connection works, the details are output to the Console window. If there is an error, the error is output to the console.
using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI;
public class Example : MonoBehaviour { int m_ServerSocket;
HostTopology m_HostTopology;
//These are the Buttons that start the client and server, and the Button for sending messages //Assure that you assign these in the Inspector before testing public Button m_ServerButton;
void Start() { //Set up the Connection Configuration which holds channel information ConnectionConfig config = new ConnectionConfig();
//Create a new Host information based on the configuration created, and the maximum connections allowed (20) m_HostTopology = new HostTopology(config, 20); //Initialise the NetworkTransport NetworkTransport.Init();
//Call the ServerButton function when you click the server Button m_ServerButton.onClick.AddListener(ServerButton); }
void Update() { //These are the variables that are replaced by the incoming message int outHostId; int outConnectionId; int outChannelId; byte[] buffer = new byte[1024]; int receivedSize; byte error;
//Set up the Network Transport to receive the incoming message, and decide what type of event NetworkEventType eventType = NetworkTransport.Receive(out outHostId, out outConnectionId, out outChannelId, buffer, buffer.Length, out receivedSize, out error);
switch (eventType) { //Use this case when there is a connection detected case NetworkEventType.ConnectEvent: { //Call the function to deal with the received information OnConnect(outHostId, outConnectionId, (NetworkError)error); break; }
case NetworkEventType.Nothing: break;
default: //Output the error Debug.LogError("Unknown network message type received: " + eventType); break; } }
//This function is called when a connection is detected void OnConnect(int hostID, int connectionID, NetworkError error) { //Output the given information to the console Debug.Log("OnConnect(hostId = " + hostID + ", connectionId = " + connectionID + ", error = " + error.ToString() + ")"); }
void ServerButton() { byte error; //Open the sockets for sending and receiving the messages on port 54321 m_ServerSocket = NetworkTransport.AddHost(m_HostTopology, 54321); //Connect the "server" NetworkTransport.Connect(m_ServerSocket, "127.0.0.1", 54321, 0, out error); //Check for if there is an error if ((NetworkError)error != NetworkError.Ok) { //Output this message in the console with the Network Error Debug.Log("There was this error : " + (NetworkError)error); } //Otherwise if no errors occur, output this message to the console else Debug.Log("Connected : " + (NetworkError)error); } }
Ok | The operation completed successfully. |
WrongHost | The specified host not available. |
WrongConnection | The specified connectionId doesn't exist. |
WrongChannel | The specified channel doesn't exist. |
NoResources | Not enough resources are available to process this request. |
BadMessage | Not a data message. |
Timeout | Connection timed out. |
MessageToLong | The message is too long to fit the buffer. |
WrongOperation | Operation is not supported. |
VersionMismatch | The protocol versions are not compatible. Check your library versions. |
CRCMismatch | The Networking.ConnectionConfig does not match the other endpoint. |
DNSFailure | The address supplied to connect to was invalid or could not be resolved. |
UsageError | This error will occur if any function is called with inappropriate parameter values. |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.