Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Resolved Connecting android client to PC server

Discussion in 'Unity Transport' started by Silas6, Dec 31, 2022.

  1. Silas6

    Silas6

    Joined:
    Nov 5, 2016
    Posts:
    4
    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:
    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Networking.Transport;
    3. using UnityEngine;
    4.  
    5. public class Server : MonoBehaviour
    6. {
    7.     public NetworkDriver m_Driver;
    8.     NetworkPipeline m_PipelineFast;
    9.     NetworkPipeline m_PipelineSlow;
    10.     NetworkPipeline m_PipelineBig;
    11.     bool acceptingConnections = true;
    12.     bool acceptingMessages = true;
    13.     private NativeList<NetworkConnection> m_Connections;
    14.  
    15.     void Start()
    16.     {
    17.         InitNew();
    18.     }
    19.  
    20.     public void InitNew()
    21.     {
    22.         m_Driver = NetworkDriver.Create();
    23.         var endpoint = NetworkEndPoint.AnyIpv4;
    24.         endpoint.Port = 9000;
    25.         if (m_Driver.Bind(endpoint) != 0)
    26.             Debug.Log("Failed to bind to port 9000");
    27.         else
    28.             m_Driver.Listen();
    29.  
    30.         m_Connections = new NativeList<NetworkConnection>(16, Allocator.Persistent);
    31.         m_PipelineBig = m_Driver.CreatePipeline(typeof(FragmentationPipelineStage), typeof(ReliableSequencedPipelineStage));
    32.         m_PipelineSlow = m_Driver.CreatePipeline(typeof(ReliableSequencedPipelineStage));
    33.         DontDestroyOnLoad(gameObject);
    34.     }
    35.  
    36.     void Update()
    37.     {
    38.         UpdateMessagesNew();
    39.     }
    40.  
    41.     public void UpdateMessagesNew()
    42.     {
    43.  
    44.         m_Driver.ScheduleUpdate().Complete();
    45.  
    46.         // Clean up connections
    47.         if (acceptingConnections)
    48.         {
    49.             for (int i = 0; i < m_Connections.Length; i++)
    50.             {
    51.                 if (!m_Connections[i].IsCreated)
    52.                 {
    53.                         m_Connections.RemoveAt(i);
    54.                 }
    55.             }
    56.  
    57.             // Accept new connections
    58.             NetworkConnection c;
    59.             while ((c = m_Driver.Accept()) != default(NetworkConnection))
    60.             {
    61.                 Debug.Log("someone is trying to connect");
    62.                 m_Connections.Add(c);
    63.                 Debug.Log("Accepted a new connection, in spot: " + (m_Connections.Length - 1));
    64.             }
    65.         }
    66.  
    67.         //get any new messages
    68.         if (acceptingMessages)
    69.         {
    70.             DataStreamReader stream;
    71.             for (int i = 0; i < m_Connections.Length; i++)
    72.             {
    73.                 NetworkEvent.Type cmd;
    74.                 while ((cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream)) != NetworkEvent.Type.Empty)
    75.                 {
    76.                     if (cmd == NetworkEvent.Type.Data)
    77.                     {
    78.  
    79.                     }
    80.                     else if (cmd == NetworkEvent.Type.Disconnect)
    81.                     {
    82.                         Debug.Log("player " + i + "is disconnecting");
    83.                         m_Connections[i] = default(NetworkConnection);
    84.                     }
    85.                 }
    86.             }
    87.         }
    88.     }
    89. }
    90.  
    Client Code
    Code (CSharp):
    1. using Unity.Networking.Transport;
    2. using UnityEngine;
    3. using TMPro;
    4.  
    5. public class Client : MonoBehaviour
    6. {
    7.     public TMP_InputField serverIP;
    8.     public GameObject ConnectNotice;
    9.     string SERVER_IP = "127.0.0.1";
    10.     public NetworkDriver m_Driver;
    11.     public NetworkConnection m_Connection;
    12.     NetworkPipeline m_PipelineFast;
    13.     NetworkPipeline m_PipelineSlow;
    14.     NetworkPipeline m_PipelineBig;
    15.     bool connected = false;
    16.  
    17.     //this gets called when the player clicks "try connecting" button
    18.     public void InitNew()
    19.     {
    20.         SERVER_IP = serverIP.text;
    21.         Debug.Log("Trying to connect to : " + SERVER_IP);
    22.         m_Driver = NetworkDriver.Create();
    23.         m_Connection = default(NetworkConnection);
    24.         _ = new NetworkEndPoint();
    25.         NetworkEndPoint endpoint;
    26.         NetworkEndPoint.TryParse(SERVER_IP, 9000, out endpoint, NetworkFamily.Ipv4);
    27.         endpoint.Port = 9000;
    28.         m_PipelineBig = m_Driver.CreatePipeline(typeof(FragmentationPipelineStage), typeof(ReliableSequencedPipelineStage));
    29.         m_PipelineSlow = m_Driver.CreatePipeline(typeof(ReliableSequencedPipelineStage));
    30.         m_Connection = m_Driver.Connect(endpoint);
    31.         connected = true;
    32.     }
    33.  
    34.     private void Update()
    35.     {
    36.         if(connected)
    37.         UpdateMessages();
    38.     }
    39.  
    40.     public void UpdateMessages()
    41.     {
    42.         m_Driver.ScheduleUpdate().Complete();
    43.         if (!m_Connection.IsCreated)
    44.         {
    45.             return;
    46.         }
    47.  
    48.         DataStreamReader stream;
    49.         NetworkEvent.Type cmd;
    50.         while ((cmd = m_Connection.PopEvent(m_Driver, out stream, out _)) != NetworkEvent.Type.Empty)
    51.         {
    52.             if (cmd == NetworkEvent.Type.Connect)
    53.             {
    54.                 Debug.Log("We are now connected to the server");
    55.                 ConnectNotice.SetActive(true);
    56.  
    57.             }
    58.             else if (cmd == NetworkEvent.Type.Data)
    59.             {
    60.  
    61.             }
    62.             else if (cmd == NetworkEvent.Type.Disconnect)
    63.             {
    64.                 Debug.Log("Client got disconnected from server");
    65.                 m_Connection = default(NetworkConnection);
    66.             }
    67.         }
    68.     }
    69. }
    70.  
     
  2. Silas6

    Silas6

    Joined:
    Nov 5, 2016
    Posts:
    4
    Solved my problem, ended up being a windows firewall thing that was blocking the connection