I am unable to connect to the server using NetworkTransport.Connect with a public IP address. After calling NetworkTransport.Connect in the Client script, in Server script NetworkTransport.Receive returns a NetworkError.Timeout error and a NetworkEventType.DisconnectEvent about every second after NetworkTransport.Connect was called (NetworkEventType.ConnectEvent was never received so how can you disconnect if you were never connected in the first place?). These scripts work just fine if I’m using localhost 127.0.0.1 for an IP.
Server script:
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System.Net.Sockets;
using System.Net;
public class Server : MonoBehaviour
{
private const int MAX_CONNECTION = 16;
private int port = 13654;
private int hostID;
private int webHostID;
private int reliableChannel;
private int unreliableChannel;
private bool isStarted = false;
private byte error;
void Start()
{
NetworkServer.Reset();
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unreliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topo = new HostTopology(cc, MAX_CONNECTION);
hostID = NetworkTransport.AddHost(topo, port, null);
isStarted = true;
}
// Update is called once per frame
void Update()
{
if (!isStarted)
return;
int recHostID;
int connectionID;
int channelID;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive(out recHostID, out connectionID, out channelID, recBuffer, bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.ConnectEvent:
OnConnection(connectionID);
break;
case NetworkEventType.DisconnectEvent:
OnDisconnection(connectionID);
break;
case NetworkEventType.DataEvent:
break;
}
if ((NetworkError)error != NetworkError.Ok)
Debug.LogError((NetworkError)error);
}
}
Client script:
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Client : MonoBehaviour {
private const int MAX_CONNECTION = 16;
private int port = 13654;
public string ip = ""; //my public IP
private int hostID;
private int webHostID;
private int reliableChannel;
private int unreliableChannel;
private int ourClientID;
private int connectionID;
private float connectionTime;
private bool isConnected = false;
private bool isStarted = false;
private byte error;
public void Connect()
{
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unreliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topo = new HostTopology(cc, MAX_CONNECTION);
hostID = NetworkTransport.AddHost(topo);
connectionID = NetworkTransport.Connect(hostID, ip, port, 0, out error);
if ((NetworkError)error != NetworkError.Ok)
Debug.LogError((NetworkError)error);
connectionTime = Time.time;
isConnected = true;
}
}
I’ve been successful in establishing a connection through the same port using system.net.sockets.udpclient in a different Unity project so I am not certain whether my code is lacking something or this is possibly a bug.