Home > Backend Development > C#.Net Tutorial > C# program to find client's IP address

C# program to find client's IP address

王林
Release: 2023-09-05 11:09:09
forward
1217 people have browsed it

C# 程序查找客户端的 IP 地址

First find the host name using Dns.GetHostName() method in C#-

String hostName = string.Empty;
hostName = Dns.GetHostName();
Console.WriteLine("Hostname: "+hostName);
Copy after login

Now, use the IPHostEntry.AddressList property to get the IP address-

IPHostEntry myIP = Dns.GetHostEntry(hostName);
IPAddress[] address = myIP.AddressList;
Copy after login

Example

Try the following code to display the IP address-

using System;
using System.Net;
class Program {
   static void Main() {
      String hostName = string.Empty;
      hostName = Dns.GetHostName();
      IPHostEntry myIP = Dns.GetHostEntry(hostName);
      IPAddress[] address = myIP.AddressList;
      for (int i = 0; i < address.Length; i++) {
         Console.WriteLine("IP Address {1} : ",address[i].ToString());
      }
      Console.ReadLine();
   }
}
Copy after login

The above is the detailed content of C# program to find client's IP address. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template