//method for obtaining list of current IP addresses, that //have connection to the internet private static List <IPtype> GetLocalIpList() { List <IPtype> IPlist = new List <IPtype>(); //checking if computer is connected to internet bool connected = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable(); if (connected) { //get list of active internet adapters NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (var adapter in interfaces) { //if it is Wi-Fi or Ethernet, add Its IP to the list if (adapter.Name == "Wi-Fi" || adapter.Name.Contains("Ethernet")) { var ipProps = adapter.GetIPProperties(); foreach (var ip in ipProps.UnicastAddresses) { //Add only if it works if ((adapter.OperationalStatus == OperationalStatus.Up) && (ip.Address.AddressFamily == AddressFamily.InterNetwork)) { IPConnectionType type = IPConnectionType.None; if (adapter.Name == "Wi-Fi") { type = IPConnectionType.WiFi; } else if (adapter.Name == "Ethernet") { type = IPConnectionType.Ethernet; } IPtype IP = new IPtype() { IPaddress = ip.Address.ToString(), ConnectionType = type, }; IPlist.Add(IP); var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Info("Founded IP address: {0}", ip.Address.ToString()); } } } } } else { //log the fact, that there is no internet connection var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Error("There is no valid internet connection"); IPlist.Clear(); } return(IPlist); }
//method that returns service endpoint //service hosted in every player computer public static Uri GetClientServiceEndpoint() { IPtype ProperIP = GetProperIPAddress(GetLocalIpList()); Uri endpoint; //if no founded addresses, or null - stop the service if (ProperIP.IPaddress != "0.0.0.0" || ProperIP.ConnectionType == IPConnectionType.None) { endpoint = new Uri(String.Concat(@"http://", ProperIP.IPaddress, @":9501/MakaoGameCliestServiceHost")); thisClientIPAddress = ProperIP.IPaddress.ToString(); var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Info("Assigned client endpoint: " + endpoint.ToString()); } else { endpoint = null; } return(endpoint); }
//method for obtaining only IP address - local address public static string GetLocalIPAddress() { IPtype ProperIP = GetProperIPAddress(GetLocalIpList()); return(ProperIP.IPaddress); }