1
TNet 3 Support / Re: IP address of iPhone in network
« 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:
.
.
.
- #if UNITY_IPHONE
- using System;
- using System.Net.NetworkInformation;
- using System.Linq;
- #endif
.
.
.
- static public IPAddress localAddress {
- get {
- if (mLocalAddress == null) {
- #if UNITY_IPHONE
- NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
- foreach (NetworkInterface ni in nis) {
- IPInterfaceProperties IPInterfaceProperties = ni.GetIPProperties();
- UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;
- foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection) {
- if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork) {
- mLocalAddress = UnicastIPAddressInformation.Address;
- break;
- }
- }
- }
- #else
- mLocalAddress = IPAddress.Loopback;
- try
- {
- IPHostEntry ent = Dns.GetHostEntry(Dns.GetHostName());
- foreach (IPAddress ip in ent.AddressList)
- {
- if (IsValidAddress(ip))
- {
- mLocalAddress = ip;
- break;
- }
- }
- }
- #if DEBUG
- catch (System.Exception ex)
- {
- System.Console.WriteLine("TNTools.LocalAddress: " + ex.Message);
- }
- #else
- catch (System.Exception) {}
- #endif
- #endif
- }
- return mLocalAddress;
- }
- }