So previously, I was coming up with a few ideas on how to create a dynamic environment for development to simplify network communications (for demonstrations in random locations and such). I came up with a No-IP solution for public connections, but I don't always want my server to be public... so here's a local host solution:
Code:
// If the server is running on the local machine for development, get the
// ip address that the machine would like to use:
#region Get Local Host
if (server.IPAddress == "localhost" || server.IPAddress.StartsWith("127"))
foreach (NetworkInterface connection in NetworkInterface.GetAllNetworkInterfaces())
{
// If the connection is from the active network adapter, get the IPv4
// address of the connection:
if (connection.OperationalStatus == OperationalStatus.Up)
{
var properties = connection.GetIPProperties();
foreach (IPAddressInformation info in properties.UnicastAddresses)
{
// If the address is of type IPv4, that's the address we're looking for!
if (info.Address.AddressFamily == AddressFamily.InterNetwork && info.Address.ToString() != "127.0.0.1")
{
server.IPAddress = info.Address.ToString();
break;
}
}
}
}
#endregion
Cheers.
- Spirited Fang







