I have two projects, one that acts as a server and another that’s for the clients. In the editor, I am able to connect them. However, when I run the client on my phone and the server on my computer it never connects. I don’t get any errors or anything.
I was able to get different computers on different networks to connect (so I know all the port forwarding stuff is working correctly) but whenever I run the same client android it doesn’t connect.
Is there something that needs to be done differently when using the transport layer with mobile vs PC?
Server code:
using Unity.Collections;
using Unity.Networking.Transport;
using UnityEngine;
public class Server : MonoBehaviour
{
public NetworkDriver m_Driver;
NetworkPipeline m_PipelineFast;
NetworkPipeline m_PipelineSlow;
NetworkPipeline m_PipelineBig;
bool acceptingConnections = true;
bool acceptingMessages = true;
private NativeList<NetworkConnection> m_Connections;
void Start()
{
InitNew();
}
public void InitNew()
{
m_Driver = NetworkDriver.Create();
var endpoint = NetworkEndPoint.AnyIpv4;
endpoint.Port = 9000;
if (m_Driver.Bind(endpoint) != 0)
Debug.Log("Failed to bind to port 9000");
else
m_Driver.Listen();
m_Connections = new NativeList<NetworkConnection>(16, Allocator.Persistent);
m_PipelineBig = m_Driver.CreatePipeline(typeof(FragmentationPipelineStage), typeof(ReliableSequencedPipelineStage));
m_PipelineSlow = m_Driver.CreatePipeline(typeof(ReliableSequencedPipelineStage));
DontDestroyOnLoad(gameObject);
}
void Update()
{
UpdateMessagesNew();
}
public void UpdateMessagesNew()
{
m_Driver.ScheduleUpdate().Complete();
// Clean up connections
if (acceptingConnections)
{
for (int i = 0; i < m_Connections.Length; i++)
{
if (!m_Connections[i].IsCreated)
{
m_Connections.RemoveAt(i);
}
}
// Accept new connections
NetworkConnection c;
while ((c = m_Driver.Accept()) != default(NetworkConnection))
{
Debug.Log("someone is trying to connect");
m_Connections.Add(c);
Debug.Log("Accepted a new connection, in spot: " + (m_Connections.Length - 1));
}
}
//get any new messages
if (acceptingMessages)
{
DataStreamReader stream;
for (int i = 0; i < m_Connections.Length; i++)
{
NetworkEvent.Type cmd;
while ((cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream)) != NetworkEvent.Type.Empty)
{
if (cmd == NetworkEvent.Type.Data)
{
}
else if (cmd == NetworkEvent.Type.Disconnect)
{
Debug.Log("player " + i + "is disconnecting");
m_Connections[i] = default(NetworkConnection);
}
}
}
}
}
}
Client Code
using Unity.Networking.Transport;
using UnityEngine;
using TMPro;
public class Client : MonoBehaviour
{
public TMP_InputField serverIP;
public GameObject ConnectNotice;
string SERVER_IP = "127.0.0.1";
public NetworkDriver m_Driver;
public NetworkConnection m_Connection;
NetworkPipeline m_PipelineFast;
NetworkPipeline m_PipelineSlow;
NetworkPipeline m_PipelineBig;
bool connected = false;
//this gets called when the player clicks "try connecting" button
public void InitNew()
{
SERVER_IP = serverIP.text;
Debug.Log("Trying to connect to : " + SERVER_IP);
m_Driver = NetworkDriver.Create();
m_Connection = default(NetworkConnection);
_ = new NetworkEndPoint();
NetworkEndPoint endpoint;
NetworkEndPoint.TryParse(SERVER_IP, 9000, out endpoint, NetworkFamily.Ipv4);
endpoint.Port = 9000;
m_PipelineBig = m_Driver.CreatePipeline(typeof(FragmentationPipelineStage), typeof(ReliableSequencedPipelineStage));
m_PipelineSlow = m_Driver.CreatePipeline(typeof(ReliableSequencedPipelineStage));
m_Connection = m_Driver.Connect(endpoint);
connected = true;
}
private void Update()
{
if(connected)
UpdateMessages();
}
public void UpdateMessages()
{
m_Driver.ScheduleUpdate().Complete();
if (!m_Connection.IsCreated)
{
return;
}
DataStreamReader stream;
NetworkEvent.Type cmd;
while ((cmd = m_Connection.PopEvent(m_Driver, out stream, out _)) != NetworkEvent.Type.Empty)
{
if (cmd == NetworkEvent.Type.Connect)
{
Debug.Log("We are now connected to the server");
ConnectNotice.SetActive(true);
}
else if (cmd == NetworkEvent.Type.Data)
{
}
else if (cmd == NetworkEvent.Type.Disconnect)
{
Debug.Log("Client got disconnected from server");
m_Connection = default(NetworkConnection);
}
}
}
}