Author Topic: Trouble with discovering local servers on ipad  (Read 4931 times)

xarasu

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Trouble with discovering local servers on ipad
« on: May 14, 2014, 12:24:16 PM »
I have an app where a user on an ipad can either create or join a server with a given access code (like NY2Z). The clients broadcast out the code to the LAN and the server is supposed to check the code with its own, then respond to the client with its ip address so the client can then connect. But the server doesn't even seem to be getting the initial broadcasted RFC. Am I not connecting in the right order? or could it be an issue with the ipads/router?


  1. public class NetworkingScript: MonoBehaviour {
  2.  
  3.   public TNObject TNO;
  4.  
  5.   public bool IsConnected {get; private set;}
  6.   public bool IsConnecting {get; private set;}
  7.  
  8.   private const int UDP_BROADCAST_PORT = 5128;
  9.   private const int TCP_CONNECTION_PORT = 5127;
  10.  
  11.   private string AccessCode;
  12.  
  13.   // Use this for initialization
  14.   void Start () {
  15.     TNManager.StartUDP(UDP_BROADCAST_PORT);
  16.   }
  17.  
  18.   public void StartServer(string accessCode) {
  19.     this.AccessCode = accessCode;
  20.     IsConnected = false;
  21.     IsConnecting = true;
  22.    
  23.     TNServerInstance.Start(TCP_CONNECTION_PORT);
  24.     TNManager.Connect("127.0.0.1", TCP_CONNECTION_PORT);
  25.   }
  26.   public void JoinServer(string accessCode) {
  27.     this.AccessCode = accessCode;
  28.     IsConnected = false;
  29.     IsConnecting = true;
  30.  
  31.     TNO.BroadcastToLAN(UDP_BROADCAST_PORT, "RequestServerWithAccessCode", accessCode);
  32.   }
  33.  
  34.   [RFC]
  35.   void RequestServerWithAccessCode(string code) {
  36.     if (IsConnecting || !TNManager.isHosting || AccessCode != code)
  37.       return;
  38.  
  39.     var ip = Network.player.ipAddress;
  40.     var serverCode = AccessCode;
  41.     TNO.BroadcastToLAN(UDP_BROADCAST_PORT, "ReturnRequestServerWithAccessCode", ip, serverCode);
  42.   }
  43.  
  44.   [RFC]
  45.   void ReturnRequestServerWithAccessCode(string ip, string code) {
  46.     if (this.AccessCode == code)
  47.       TNManager.Connect(ip, TCP_CONNECTION_PORT);
  48.   }
  49.  
  50.   void OnNetworkConnect(bool success, string message) {
  51.     if (!success)
  52.       Debug.Log("Error connecting to Server: "+message);
  53.     Debug.Log("Connected to server in channel "+TNManager.channelID);
  54.  
  55.     IsConnected = true;
  56.     IsConnecting = false;
  57.   }
  58. }
  59.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Trouble with discovering local servers on ipad
« Reply #1 on: May 15, 2014, 12:40:19 AM »
You start the server, specifying only the TCP port. You are not requesting UDP to be active. Your UDP is started only on the client side (TNManager.StartUDP). So I'm going to assume by "server" you actually meant "host" -- the server-hosting player.

IsConnected/IsConnecting is unnecessary. TNManager.isConnected and TNManager.isTryingToConnect will already tell you this.

I'm also not quite sure what the point of all this is. Why don't you use a lobby server to broadcast your server's existence to other clients? That's what it's designed to do, and is exactly what TNet's menu example uses as well. You can retrieve the list of known servers from it at any time, and connect to one of them if you so desire.

xarasu

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Trouble with discovering local servers on ipad
« Reply #2 on: May 23, 2014, 12:35:41 PM »
Yes, I do mean "Host". and "IsConnected/IsConnecting" were artifacts from when I was using the pure Unity networking code, but thanks for the heads up. I've changed over to using the udp lobby for discovering hosts on the LAN and it works fine when I'm running a PC build and unityEditor build. After I found that you need to join a channel to send out rfc messages? they can discover each other fine and connect. However, when I create an iPad build, they can't see each other or the pc builds. Do I need to do anything special for the iPad?

Current Networking Code (trimmed of unnecessary parts):
When the game wants to host, it calls StartServer, when they want to join a game, it calls JoinServer with an access code(this is purely a design choice)
  1. public class Networking : MonoBehaviour {
  2.  
  3.   /**
  4.    * The number of additional times to retry connecting to host
  5.    * */
  6.   public int NumJoinAttempts = 3;
  7.  
  8.   /**
  9.    * Time (in seconds) between each reconnection attempts
  10.    * */
  11.   public int TimeoutTime = 10;
  12.  
  13.   public int serverTcpPort = 5127;
  14.  
  15.   // Use this for initialization
  16.   void Start () {
  17.     TNManager.StartUDP(Random.Range(10000, 50000));
  18.   }
  19.  
  20.   void OnApplicationQuit(){
  21.     TNServerInstance.Stop("server.dat");
  22.   }
  23.  
  24.   public void StartServer(string accessCode) {
  25.     int udpPort = Random.Range(10000, 40000);
  26.     TNUdpLobbyClient lan = GetComponent<TNUdpLobbyClient>();
  27.     int lobbyPort = (lan !=null) ? lan.remotePort : 0;
  28.     var ip = Network.player.ipAddress;
  29.     var name = accessCode+"%"+ip;
  30.     TNServerInstance.serverName = name;
  31.     TNServerInstance.Start(serverTcpPort, udpPort, "server.dat", lobbyPort);
  32.     TNManager.Connect(ip, serverTcpPort);
  33.   }
  34.  
  35.   public void JoinServer(string accessCode) {
  36.     StartCoroutine(AttemptJoinServer(accessCode, 0));
  37.   }
  38.  
  39.   IEnumerator AttemptJoinServer(string accessCode, int attempt) {
  40.     // Check currently known servers for given access code
  41.     var list = TNLobbyClient.knownServers.list;
  42.     foreach (var server in list) {
  43.       var splitName = server.name.Split('%');
  44.       if (splitName[0] == accessCode) {
  45.         var ip = splitName[1];
  46.         TNManager.Connect(ip, serverTcpPort);
  47.         yield break;
  48.       }
  49.     }
  50.     yield return new WaitForSeconds(TimeoutTime);
  51.  
  52.     // Check if already connected to the server
  53.     if (TNManager.isConnected || TNManager.isTryingToConnect)
  54.       yield break;
  55.  
  56.     if (++attempt <=NumJoinAttempts) {
  57.       OnTimeOutHandlers(true, attempt, NumJoinAttempts);
  58.       StartCoroutine(AttemptJoinServer(accessCode, attempt));
  59.     }
  60.     else
  61.       OnTimeOutHandlers(false, attempt, NumJoinAttempts);
  62.   }
  63.  
  64.   void OnNetworkConnect(bool success, string message) {
  65.     if (!success) return;
  66.     TNManager.JoinChannel(1, "");
  67.   }
  68.  
  69.   void OnNetworkJoinChannel(bool success, string message) {
  70.     if (!success) return;
  71.  
  72.     if (!TNManager.isHosting) {
  73.       OnJoinChannelHandlers(true);
  74.     }
  75.   }
  76. }

xarasu

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Trouble with discovering local servers on ipad
« Reply #3 on: May 23, 2014, 01:58:07 PM »
Also built out the TNet example menu with the chat scene. On both wifi networks, the iPads didn't 'discover' each other's servers, but on one of them, I could connect to chat by directly putting in the ip address / port. On the other, I couldn't connect at all.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Trouble with discovering local servers on ipad
« Reply #4 on: May 23, 2014, 06:24:00 PM »
If TNet's example isn't able to see servers but you can still connect directly, it implies that UDP broadcasts don't work, since the server discovery is done via UDP broadcasts. This may be disabled due to security reasons. Try hosting on the PC and see if your iPads can see it.

xarasu

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Trouble with discovering local servers on ipad
« Reply #5 on: May 27, 2014, 11:15:18 AM »
Even hosting the game on the pc doesn't help, but direct connections work to any of the devices. I'm assuming this means I'll need a dedicated tcp lobby with a static ip that all my clients will connect to? Is the TNServer.exe able to handle that by itself?

Like:
  1. TNServer.exe -tcpLobby 5129
« Last Edit: May 27, 2014, 12:06:15 PM by xarasu »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Trouble with discovering local servers on ipad
« Reply #6 on: May 28, 2014, 07:57:40 AM »
Yup. Host that lobby somewhere accessible like Amazon's free EC2 server instance, and have your game servers register with it.