How to get ip address in C#?

PHPz
Release: 2023-09-01 08:09:04
forward
728 people have browsed it

How to get ip address in C#?

#The IP (Internet Protocol) address is the address of your network hardware. It helps connect your computer to other devices on the network and around the world. IP addresses are made up of numbers or characters.

Every device connected to an internet connection has a unique IP address, which means billions of IP addresses are needed. The new version of IP, IPv6, meets this requirement.

Private IP Address

A private IP address is the address of a device connected to your home or home. business network. If you have multiple different devices connected to one ISP (Internet Service Provider), then all of your devices will have unique private IP addresses. This IP address is not accessible from devices outside your home or business network.

For example: 192.168.1.1

Example

class Program{ static void Main(string[] args){ string IPAddress = GetIPAddress(); System.Console.WriteLine(IPAddress); Console.ReadLine(); } public static string GetIPAddress(){ string IPAddress = string.Empty; IPHostEntry Host = default(IPHostEntry); string Hostname = null; Hostname = System.Environment.MachineName; Host = Dns.GetHostEntry(Hostname); foreach (IPAddress IP in Host.AddressList){ if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork){ IPAddress = Convert.ToString(IP); } } return IPAddress; } }
Copy after login

Output

192.168.1.1
Copy after login

The above is the detailed content of How to get ip address in C#?. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!