Author Topic: LAN Server Creation Problem IOS mainly  (Read 9363 times)

bariscigal

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 15
    • View Profile
LAN Server Creation Problem IOS mainly
« on: August 02, 2014, 09:50:45 AM »
Hello i am only having this problem on IOS platform.

I am a starter on Tnet but trying to achieve a design of a closed game environment for 1 vs 1 game with inter-platform (android-ios) gameplay.

I can start the server without any problems on Mac and Android with this line of code :

  1. if(!TNServerInstance.isActive){
  2.   TNServerInstance.Start(5127,5113,5128,"server.dat",TNServerInstance.Type.Lan);
  3.   //Start sending server info through udp to listeners
  4.   //StartCoroutine("LanSendServerInfoUDP");
  5. }
  6.  

I have IOS pro and Unity pro. And for my android basic license i am using Good Ol' Sockets.

On ios when i start the server with an ngui button event server is not starting and this error is getting logged to console

Quote
- Completed reload, in  0.263 seconds
Non platform assembly: /private/var/mobile/Applications/4121A2A5-128A-41FF-AD43-12AE0DEBC5E6/networkTest.app/Data/Managed/System.Core.dll (this message is harmless)
_wapi_setsockopt: Need to translate 45 [Operation not supported] into winsock error
SocketException: System call failed
  at System.Net.Sockets.Socket.SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, System.Object optionValue) [0x00000] in <filename unknown>:0

  at TNet.UdpProtocol.Start (Int32 port) [0x00062] in /Users/bariscigal/networkTest/Assets/TNet/Common/TNUdpProtocol.cs:114
  at TNet.UdpLobbyServer.Start (Int32 listenPort) [0x00011] in /Users/bariscigal/networkTest/Assets/TNet/Server/TNUdpLobbyServer.cs:49
  at TNServerInstance.StartLocal (Int32 tcpPort, Int32 udpPort, System.String fileName, Int32 lobbyPort, Type type) [0x00041] in /Users/bariscigal/networkTest/Assets/TNet/Client/TNServerInstance.cs:177
  at TNServerInstance.Start (Int32 tcpPort, Int32 udpPort, Int32 lobbyPort, System.String fileName, Type type) [0x00000] in /Users/bariscigal/networkTest/Assets/TNet/Client/TNServerInstance.cs:147
  at serverStartup.StartServer () [0x0000a] in /Users/bariscigal/networkTest/Assets/serverStartup.cs:45
  at EventDelegate.Execute () [0x0001c] in /Users/bariscigal/networkTest/Assets/NGUI/Scripts/Internal/EventDelegate.cs:450
  at EventDelegate.Execute (System.Collections.Generic.List`1 list) [0x0001b] in /Users/bariscigal/networkTest/Assets/NGUI/Scripts/Internal/EventDelegate.cs:592
  at UIButton.OnClick () [0x00021] in /Users/bariscigal/networkTest/Assets/NGUI/Scripts/Interaction/UIButton.cs:201
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
UICamera:Notify(GameObject, String, Object) (at /Users/bariscigal/networkTest/Assets/NGUI/Scripts/UI/UICamera.cs:832)
UICamera:ProcessTouch(Boolean, Boolean) (at /Users/bariscigal/networkTest/Assets/NGUI/Scripts/UI/UICamera.cs:1508)
UICamera:ProcessTouches() (at /Users/bariscigal/networkTest/Assets/NGUI/Scripts/UI/UICamera.cs:1193)
UICamera:Update() (at /Users/bariscigal/networkTest/Assets/NGUI/Scripts/UI/UICamera.cs:970)
 
(Filename: /Users/bariscigal/networkTest/Assets/TNet/Common/TNUdpProtocol.cs Line: 114)

I am not using any lobby client nor predefined script of tnet. Instead i am trying to learn how to control the tnet to my needs.

As i see the problem arise from winsock but here is my UDP message sending and listening code also:

  1. void Start () {
  2.  
  3.                 // Start resolving IPs
  4.                 Tools.ResolveIPs();
  5.                 // We don't want mobile devices to dim their screen and go to sleep while the app is running
  6.                 Screen.sleepTimeout = SleepTimeout.NeverSleep;
  7.                        
  8.                 // Make it possible to use UDP using a small random range of port
  9.                 TNManager.StartUDP(Random.Range(10000,10030));
  10.  
  11.                 //if the random port gets locked by other users port than randomize once more
  12.                 while(TNManager.canUseUDP){
  13.                         TNManager.StartUDP(Random.Range(10000,10030));
  14.                 }
  15.  
  16.                 //UdpProtocol.defaultNetworkInterface = Tools.localAddress;
  17.                 tnobj = GetComponent<TNObject>();
  18.                 ServerName = SystemInfo.deviceName;
  19.                 directConnectionIp = Tools.localAddress;
  20.                 ServerIpStr = directConnectionIp.ToString();
  21.         }
  22. .
  23. .
  24. .
  25. public IEnumerator LanSendServerInfoUDP()
  26.         {
  27.                 while(true){
  28.                         //start broadcasting the server info from udp through lan broadcast
  29.                         yield return new WaitForSeconds(1.5f);
  30.                         message = ServerName + '@' + ServerIpStr;
  31.  
  32.                         for(int i = 10000;i<= 10030;i++){
  33.                                 tnobj.BroadcastToLAN(i,"ReceiveServerInfo",message);
  34.                         }
  35.  
  36.                         yield return null;
  37.                 }
  38.         }
  39. .
  40. .
  41. .
  42. [RFC]
  43.         public void ReceiveServerInfo(string broadcastServerInfo){
  44.                 string[] responseMessage = broadcastServerInfo.Split(new char[] { '@' }, System.StringSplitOptions.RemoveEmptyEntries);
  45.                 IpToConnect = responseMessage[1] ;
  46.                 stringToEditRcv = "message received  " + responseMessage[1] ;
  47.         }
  48.  
  49.  


I would be glad if someone can help me point what i am doing wrong.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: LAN Server Creation Problem IOS mainly
« Reply #1 on: August 03, 2014, 10:21:55 AM »
Why do you have that coroutine that does broadcasts inside? A broadcast is just that -- it goes to everyone. All your clients should listen to the same UDP port, so the broadcast should reach them. I'm not sure why you have a port range like that.

As for the error, can't say I've seen that and Starlink was published on iOS as well without issues (and could host games there). Two things to note -- make sure you aren't using stripping of any kind, and don't try to do UDP via a carrier connection (3G etc). Only WiFi supports UDP.

bariscigal

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: LAN Server Creation Problem IOS mainly
« Reply #2 on: August 03, 2014, 01:37:53 PM »
For the coroutine it is just there to keep sending the servers info (name and local ip of the broadcaster) in a 1.5s of wait time between broadcast signals.
I think i would have done that with invoke repeating but i don't know a way to stop that function working when broadcast needs to be finished.

Thank you i will look in to this problem more.
I will let others know when i found a way to this weird problem. I checked the things you have mentioned before posting.

bariscigal

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: LAN Server Creation Problem IOS mainly
« Reply #3 on: August 04, 2014, 03:07:55 AM »
This solved the problem

  1. UdpProtocol.useMulticasting = false;

Any ideas why Multicasting is causing Udp to give those errors?

Now it works with GoS and Ios Pro Sockets.

PS:
I also needed to add this
  1. // Start resolving IPs
  2.                 Debug.Log(Tools.localAddress);

because of the 255.255.255.255 ip resolving problem. Added to Start()
« Last Edit: August 04, 2014, 04:31:05 AM by bariscigal »

Ferazel

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 150
    • View Profile
Re: LAN Server Creation Problem IOS mainly
« Reply #4 on: August 05, 2014, 06:43:21 PM »
Hmm, I think I might be having a similar problem or it is related. I tried turning off multicasting and still having a problem. I also have similar requirements in my project (1v1 LAN).

To replicate:
Using the example scenes in a fresh Unity 4.5.2 project I deployed to my iPhone5 (7.1.2). I created a new project from the sample scene (set Example Menu) as the 0 ID. Deployed on my iPhone. It loads and launches fine. I can create a server on my iPhone and it shows up on the server list but I'm unable to connect either on the phone itself or from an editor client. It works fine from an iPad, but my iPhone is having problems.

Can you replicate this problem ArenMook when you get a chance?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: LAN Server Creation Problem IOS mainly
« Reply #5 on: August 06, 2014, 08:07:08 PM »
@bariscigal: Multicasting is one of those features that isn't guaranteed to be supported by the router properly, which is why there is a way to turn it off. Still the error you got is quite strange and I wouldn't have guessed that it was in any way related. Then again, it's not very descriptive, so who knows...

@Ferazel: Is it only on the iPhone? Is it like http://www.tasharen.com/forum/index.php?topic=10694.0 ?

Ferazel

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 150
    • View Profile
Re: LAN Server Creation Problem IOS mainly
« Reply #6 on: August 07, 2014, 10:43:56 AM »
@ArenMook - Yes, it is only iPhones that are having this problem. We don't have an iPad with a cell data plan (WiFi only) so it might be a problem there as well. It is similar in that it will take awhile trying to connect and then produce the error "Unable to connect". However, I'm not use "Good Olde Sockets" I'm using Unity iOS pro and using the basic TNET sample. I turned off my phone's cellular data in Settings and it worked. However, expecting our users to turn off their cell data before hosting a game isn't going to be something that is going to work very well. Are there any other solutions or recommendations you can provide for this problem?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: LAN Server Creation Problem IOS mainly
« Reply #7 on: August 08, 2014, 06:52:09 AM »
Have you tried running it without it being linked to XCode with proper provisioning in place? I can't think of any reason why you'd need to turn off carrier data considering you mentioned that you don't actually have a data plan, implying it's disabled anyway. What does Unity report via Application.internetReachability?

Ferazel

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 150
    • View Profile
Re: LAN Server Creation Problem IOS mainly
« Reply #8 on: August 08, 2014, 01:19:09 PM »
Just to clarify.

On both my iPhone5 and my co-worker's iPhone5c (and I assume other iPhones with active data plans) we can't connect to a hosted server on our devices unless we first disable the cell data option inside of the Settings App. We can host a server initially just fine. The server will show up in the Editor and other devices known servers list via the UDPLobbyClient. So Application.internetReachability shouldn't be a problem since it sends the information over the network. However, nothing can connect to the server, not even the iPhone where the server was hosted. It will either time out or produce the error "Unable to connect" and sometimes I receive "The object was used after being disposed". 

The LAN and WAN IP address that is being produced in the sample project is coming from the WiFi Router as confirmed from WhatIsMyIp.com on a desktop computer on the same Wifi network.

We don't have a cell data network 3G/LTE iPad to test on (we only use WiFi iPads in our testing so far), so it might be happening on iPads with multiple NICs as well.

I'm using the default TNET sample projects in Unity iOS Pro 4.5.2.

I have tried to do this with and without XCode connected and it is producing the same results.

We haven't done any Android tests yet, so we can't comment if this problem is present on Android phones.

I wouldn't be surprised if this was a Unity iOS Mono bug, but I don't really know way to test it with a newer version.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: LAN Server Creation Problem IOS mainly
« Reply #9 on: August 08, 2014, 05:30:37 PM »
I'm at a loss as to why it would happen in your case. As I mentioned I see no reason why inactive carrier data would affect anything in your case. With the carrier data disabled, you should only have one interface to work with anyway. Perhaps it doesn't? But even so, TCP connections work regardless of how many NICs are present. It's only UDP broadcasts that are iffy -- so if anything, I'd expect the opposite to work: able to connect, but not able to see the LAN server. Try creating a simple socket and connecting to something, see if that works. If it doesn't, then it's some Unity bug you might need to report.

P.S. And I'd still be interested to know what the internetReachability reports.

Ferazel

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 150
    • View Profile
Re: LAN Server Creation Problem IOS mainly
« Reply #10 on: September 05, 2014, 10:29:04 AM »
To reiterate the problem, we're using the TNET sample menu included with the TNET package. On iOS with a device with both a WiFi and cell data network connection it will start up a server fine and will show up in the lobby list for all devices. However, when you try to connect to the server it starts using either itself or another device (that can see it in the lobby server) it will time out or not connect. This affects both iPhones and iPads have active cellular data networks when you use the cell data NIC. If you turn off the cell data network it will work fine, but asking users to do this in order to host a LAN game is not a desirable user experience. 

We tested the same thing on an Android phone with Cell Data active (and WiFi disabled) and it doesn't have a problem hosting through the cell network. This leads us to believe that it might be a Mono bug with the iOS implementation.

A co-worker of mine was able to spend a significant amount of time trying to track down a problem. We determined a workaround for this problem but not the underlying problem.

He created a test script:

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Net.Sockets;
  4. using System.Net;
  5. using System.Threading;
  6.  
  7. public class TestScript : MonoBehaviour {
  8.  
  9.     private int kListenPort = 5129;
  10.     private TcpListener _listener;
  11.  
  12.     // Use this for initialization
  13.     void Start () {
  14.         StartCoroutine (CoroutineStartTest ());
  15.     }
  16.  
  17.     private IEnumerator CoroutineStartTest() {
  18.         StartServer ();
  19.         yield return new WaitForSeconds (1);
  20.         ClientConnect (new IPEndPoint(TNet.Tools.localAddress, kListenPort)); //LOCAL ADDRESS[0] IS CELL DATA
  21.         ClientConnect (new IPEndPoint(TNet.Tools.localAddresses[1], kListenPort)); //WILL WORK AS [1] IS WIFI
  22.     }
  23.  
  24.     private void StartServer() {
  25.         _listener = new TcpListener (IPAddress.Any, kListenPort);
  26.         Debug.Log ("Starting listener...");
  27.         _listener.Start(5);
  28.  
  29.         Thread thread = new Thread (ThreadFunc);
  30.         thread.Start ();
  31.     }
  32.  
  33.     private void ThreadFunc() {
  34.         Debug.Log ("Starting Thread function...");
  35.         Socket socketConnect = null;
  36.         while (true) {
  37.             if (_listener.Pending()) {
  38.                 socketConnect =    _listener.AcceptSocket();
  39.                 Debug.Log ("Socket Connection established: " + socketConnect.LocalEndPoint);
  40.             }
  41.         }
  42.     }
  43.  
  44.     private void ClientConnect(IPEndPoint endPoint) {
  45.         if (endPoint != null) {
  46.             string error = "(None)";
  47.             try {
  48.                 Debug.Log("Creating client socket...");
  49.                 Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  50.                 Debug.Log("Connecting at " + endPoint + "...");
  51.                 socket.Connect (endPoint);
  52.             } catch (System.Exception ex) {
  53.                 error = ex.Message;
  54.             } finally {
  55.                 Debug.Log("Finished Connect. Error: " + error);
  56.             }
  57.         }
  58.     }
  59. }
  60.  

It seems that there might be a iOS mono bug with connecting to a cell data's IP address, that goes into unmanaged code and becomes very difficult to track down.

The workaround that he came up with is the network interface type will return UNKNOWN for cell data, and we ignore all network interfaces with UNKNOWN so that we can make sure that we don't include the cell data IP in the list of local addresses. So to work around this problem we put this code into the TNTools.localAddresses property.

  1.  
  2. for (int i = 0; i < list.size; ++i)
  3. {
   
  4.        NetworkInterface ni = list[i];
  5. 
     if (ni == null) continue;
  6.         IPInterfaceProperties props = ni.GetIPProperties();
  7.         if (props == null) continue;
  8.         if(ni.NetworkInterfaceType == NetworkInterfaceType.Unknown) {
  9. 
             continue;
  10. 
     }
  11.         ….
  12. }
  13.  

This will prevent the local addresses from using the cell data network and require WIFI to prevent this from happening.

Curiously, the iPad with cellular that we tested exhibited the same problem, but the order of the network interfaces was different so that it put the WiFi network adapter in index 0 which works fine. However, to be clear, the iPad would exhibit the same connection problems if we disabled the WiFi and it was forced to use the cell data's IP.

Please let me know if you have any further questions or if you need additional clarification.
« Last Edit: September 05, 2014, 02:05:56 PM by Ferazel »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: LAN Server Creation Problem IOS mainly
« Reply #11 on: September 05, 2014, 01:34:17 PM »
Thanks! I will add the Unknown check to the next version.

Ferazel

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 150
    • View Profile
Re: LAN Server Creation Problem IOS mainly
« Reply #12 on: September 05, 2014, 02:25:05 PM »
I'm not sure if you want to include this in the raw code base because I assume other users may want to be able to connect to remote game servers via a cell network (if it was hosted on Amazon for example). I would guess that the device needs a valid local IP to connect which would not be the case if that was the only active connection. We are only looking for LAN functionality currently, so it is not a problem for us to ignore the IP address of this NIC. I'm not sure if TNET needs a local IP to connect to an external server in this case.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: LAN Server Creation Problem IOS mainly
« Reply #13 on: September 07, 2014, 08:56:18 AM »
Do you still get "Unknown" if you're actually connected to the cell network though?

Ferazel

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 150
    • View Profile
Re: LAN Server Creation Problem IOS mainly
« Reply #14 on: September 12, 2014, 07:26:08 PM »
Yes, the adapter is always of type "Unknown" even when connected via cell data.