Author Topic: IP address of iPhone in network  (Read 9803 times)

Adriaan

  • Guest
IP address of iPhone in network
« on: May 26, 2013, 03:58:46 PM »
Hi,

This is probably a real simple question, but I haven't figured out yet how to get around it.

If I create a server on my iPhone, it will always say 'TNet.Tools.localAddress' is 127.0.0.1:port. But of course, if I'd want to connect to my iPhone with my computer or another phone, that is NOT the address I should try to connect, am I right? How do I get the right IP address from within the network?

Thanks a lot!

Adriaan

Adriaan

  • Guest
Re: IP address of iPhone in network
« Reply #1 on: May 26, 2013, 06:11:32 PM »
I mean, I don't want to be going to my network setting on my phone every time I walk out of the WIFI signal to check what my IP address is... There must be a way for Unity / TNet to know the client IP of the network, right?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: IP address of iPhone in network
« Reply #2 on: May 26, 2013, 06:50:36 PM »
TNet.Tools.externalAddress is what you are looking for.

Adriaan

  • Guest
Re: IP address of iPhone in network
« Reply #3 on: May 26, 2013, 07:03:01 PM »
Unfortunately not - that gives me the address of my routers internet connection.

I'm looking for the ip address of my phone inside my home network. Localaddress returns 127.0.0.1 and the externaladdress some IP starting with 87.etc. The one i'm looking for should start with 192.168.etc.

I know for sure that is I start a server on my phone, it CAN be reached through an IP starting with 192 from another device. It's just that the localAddress returns the wrong thing...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: IP address of iPhone in network
« Reply #4 on: May 26, 2013, 07:05:19 PM »
TNet.Tools.localAddress gives you that if it's able to determine it. It will be 127.0.0.1 if it can't. Just look inside the function to see what's happening. TNet's code is open for a reason, and it's really short.

Adriaan

  • Guest
Re: IP address of iPhone in network
« Reply #5 on: May 27, 2013, 05:07:01 PM »
So, what do you suggest I do beside scripting a whole lobby? I cannot let any client connect to 127.0.0.1 or to the external address 87.210.214.149 - is there a way to know the IP address of the iPhone on my routers network? (Looking in the source code doesn't help answering this question)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: IP address of iPhone in network
« Reply #6 on: May 28, 2013, 01:55:44 AM »
My question is why it's not being able to determine the local IP. I've never seen that happen -- even when playing via 3G. Since you are the only one who has experienced this, only you can figure out why this happens by looking at the code and possibly adding some Debug.Log statements.

Oh and make sure you grab the latest version of TNet as well. There were changes to that logic recently.

EdLan

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: IP address of iPhone in network
« Reply #7 on: May 28, 2013, 04:38:56 PM »
I have same issue... Could it be related to that problem they mention here? :

http://wiki.etc.cmu.edu/unity3d/index.php/Network_issue_on_iOS


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: IP address of iPhone in network
« Reply #8 on: May 28, 2013, 05:38:46 PM »
Hmm... interesting. I was not aware of that. Well, try replacing the TNTools.localAddress function with this:
  1.         static public IPAddress localAddress
  2.         {
  3.                 get
  4.                 {
  5.                         if (mLocalAddress == null)
  6.                         {
  7. #if UNITY_IPHONE
  8.                                 mLocalAddress = IPAddress.Parse(UnityEngine.Network.player.ipAddress);
  9. #else
  10.                                 mLocalAddress = IPAddress.Loopback;
  11.  
  12.                                 try
  13.                                 {
  14.                                         IPHostEntry ent = Dns.GetHostEntry(Dns.GetHostName());
  15.  
  16.                                         foreach (IPAddress ip in ent.AddressList)
  17.                                         {
  18.                                                 if (IsValidAddress(ip))
  19.                                                 {
  20.                                                         mLocalAddress = ip;
  21.                                                         break;
  22.                                                 }
  23.                                         }
  24.                                 }
  25. #if DEBUG
  26.                                 catch (System.Exception ex)
  27.                                 {
  28.                                         System.Console.WriteLine("TNTools.LocalAddress: " + ex.Message);
  29.                                 }
  30. #else
  31.                                 catch (System.Exception) {}
  32. #endif
  33. #endif
  34.                         }
  35.                         return mLocalAddress;
  36.                 }
  37.         }

EdLan

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: IP address of iPhone in network
« Reply #9 on: May 29, 2013, 07:57:53 AM »
Nope... Now it crashes.

With you mentioned solution we get the following error message when trying to addd a new server to the lobby server:

  1. Internal_GetPlayer  can only be called from the main thread.
  2. Constructors and field initializers will be executed from the loading thread when loading a scene.
  3. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

EdLan

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: IP address of iPhone in network
« Reply #10 on: May 29, 2013, 09:23:36 AM »
You may have to do something like the following code. Of course you have to refine that stuff, because there can be more that one NIC with a IPv4 Address attached to (or inside of) a particular device and for that you may have to take care:

  1.         static public IPAddress localAddress {
  2.        
  3.                 get {
  4.        
  5.                     List<IPAddress> addressList = new List<IPAddress>();
  6.  
  7.                     NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
  8.  
  9.                     foreach (NetworkInterface NetworkIntf in NetworkInterfaces) {
  10.  
  11.                         IPInterfaceProperties IPInterfaceProperties = NetworkIntf.GetIPProperties();
  12.                         UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;
  13.  
  14.                         foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection) {
  15.  
  16.                             if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork) {
  17.  
  18.                                 //Console.WriteLine(UnicastIPAddressInformation.Address);
  19.  
  20.                                 addressList.Add(UnicastIPAddressInformation.Address);
  21.                                                         mLocalAddress = UnicastIPAddressInformation.Address;
  22.                             }
  23.                         }
  24.                     }
  25.                         //return addressList;
  26.                         return mLocalAddress;  
  27.                 }
  28.                
  29. }

I hope this helps you a little bit to fix that annoying bug.

Cheers, EdLan

P.S. Tested on OS X 10.8, iOS 6. On Android 4.2.x this version seems to make problems (I didn't had a deeper look at that right now). So may you have to implement both (refined) versions with precompiler conditions, or something like that.

BTW the mentioned version which seems to work on iOS uses:

using System;
using System.Net.NetworkInformation;
using System.Linq;

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: IP address of iPhone in network
« Reply #11 on: May 29, 2013, 11:23:30 AM »
With EdLan's suggestion:
  1.         static public IPAddress localAddress
  2.         {
  3.                 get
  4.                 {
  5.                         if (mLocalAddress == null)
  6.                         {
  7.                 NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
  8.  
  9.                 foreach (NetworkInterface ni in nis)
  10.                                 {
  11.                     IPInterfaceProperties IPInterfaceProperties = ni.GetIPProperties();
  12.                     UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;
  13.  
  14.                     foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
  15.                                         {
  16.                         if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
  17.                                                 {
  18.                             mLocalAddress = UnicastIPAddressInformation.Address;
  19.                                                         break;
  20.                         }
  21.                     }
  22.                 }
  23.                         }
  24.                         return mLocalAddress;
  25.                 }
  26.         }
« Last Edit: May 29, 2013, 11:29:07 AM by ArenMook »

EdLan

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: IP address of iPhone in network
« Reply #12 on: May 29, 2013, 03:07:17 PM »
For all where need a cross platform solution I suggest to use some code mentioned below because of platform specific problems. iOS has problems with Dns.GetHostEntry(Dns.GetHostName()) solution and Android devices have problems with the NetworkInterface.GetAllNetworkInterfaces() solution. So this should work on all platforms:

  1. #if UNITY_IPHONE
  2. using System;
  3. using System.Net.NetworkInformation;
  4. using System.Linq;
  5. #endif

.
.
.

  1.         static public IPAddress localAddress {
  2.        
  3.         get {
  4.                                
  5.             if (mLocalAddress == null) {
  6.  
  7. #if UNITY_IPHONE       
  8.                                        
  9.                 NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
  10.  
  11.                 foreach (NetworkInterface ni in nis) {
  12.                                                
  13.                     IPInterfaceProperties IPInterfaceProperties = ni.GetIPProperties();
  14.                     UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;
  15.  
  16.                     foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection) {
  17.                                                        
  18.                         if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork) {
  19.                                                                
  20.                             mLocalAddress = UnicastIPAddressInformation.Address;
  21.                             break;
  22.                                                                
  23.                         }
  24.                                                        
  25.                     }
  26.                                                
  27.                 }
  28.  
  29. #else
  30.                                        
  31.                                 mLocalAddress = IPAddress.Loopback;
  32.  
  33.                                 try
  34.                                 {
  35.                                         IPHostEntry ent = Dns.GetHostEntry(Dns.GetHostName());
  36.  
  37.                                         foreach (IPAddress ip in ent.AddressList)
  38.                                         {
  39.                                                 if (IsValidAddress(ip))
  40.                                                 {
  41.                                                         mLocalAddress = ip;
  42.                                                         break;
  43.                                                 }
  44.                                         }
  45.                                 }
  46.         #if DEBUG
  47.                                 catch (System.Exception ex)
  48.                                 {
  49.                                         System.Console.WriteLine("TNTools.LocalAddress: " + ex.Message);
  50.                                 }
  51.         #else
  52.                                 catch (System.Exception) {}
  53.         #endif
  54.        
  55. #endif
  56.                                        
  57.             }
  58.        
  59.             return mLocalAddress;
  60.                                
  61.         }
  62.                
  63.         }
« Last Edit: May 29, 2013, 03:12:18 PM by EdLan »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: IP address of iPhone in network
« Reply #13 on: May 29, 2013, 06:10:53 PM »
Just a quick note: Linq isn't needed.