参考: https://blog.csdn.net/ropin_os/article/details/6689626
电脑做hotspot时可以使用以下代码获取本地ip,用于创建socket.
需要使用using System.Net.NetworkInformation;
NetworkInterface[] interfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
int len = interfaces.Length;
for (int i = 0; i < len; i++)
{
NetworkInterface ni = interfaces[i];
if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
{
if (ni.Name.IndexOf("本地连接") != -1 || ni.Name.IndexOf("Local Area Connection") != -1)
{
IPInterfaceProperties property = ni.GetIPProperties();
foreach (UnicastIPAddressInformation ip in property.UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
if (ip.Address.ToString().IndexOf("169.254.") == -1)
{
Console.WriteLine(ip.Address.ToString());
}
}
}
}
}
}