私は現在、あるコンピューターを別のコンピューターに LAN で接続するための C# プログラムを作成しています。受信コンピューターのコンピューター名はわかっていますが、IP は動的であるため、時々変更されます。
受信側コンピューターの LAN IP アドレスを取得するにはどうすればよいですか? ( 192.168.1.# のようなもの)
仮定に基づいて、最初の IPv4 IP アドレスを探していると仮定すると、次を使用できます。
String name = "Name";
IPHostEntry ipHostInfo = Dns.GetHostEntry(name);
// OR you can get the name of the current computer using
// IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
// Get the first IPv4 address
IPAddress ip = ipHostInfo.AddressList.Where(n => n.AddressFamily == AddressFamily.InterNetwork).First();
次のようにホスト名をIPに解決できます
string hostName = "www.Google.com";
IPAddress[] addresslist = Dns.GetHostAddresses(hostName);
foreach (IPAddress address in addresslist)
{
string ip = address.ToString();
}