C# 获取本地IP地址以及MAC地址

 
C# 获取本地 IP 地址以及 MAC 地址
 
1、通过主机名获取局域网IP地址;
            try
            {
                IPHostEntry ipHostEntry = Dns.GetHostEntry(txtHost.Text);//传递计算机名
                if (ipHostEntry.Aliases.Length > 0)
                {
                    foreach (string alias in ipHostEntry.Aliases)
                        txtIP.Text = alias;
                }
                //获取IP地址
                foreach (IPAddress addr in ipHostEntry.AddressList)
                    txtIPAddress.Text = addr.ToString();//获取IP地址
            }
            catch
            {
                MessageBox.Show("错误的主机名。");
            }
2、通过局域网IP地址获取主机名;
try
            {
               IPHostEntry ipHostEntry = Dns.GetHostByAddress(txtIP.Text); ;//传递IP地址
               txtHostName.Text = ipHostEntry.HostName.ToString()//取得主机名
            }
            catch
            {
               MessageBox.Show("错误的IP地址。");
            }
3、通过局域网IP地址获取Mac地址
public static string ReadMac(string ip)//传递IP地址,即可返回MAC地址
        {
            string mac = "";
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "nbtstat";
            p.StartInfo.Arguments = "-a " + ip;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardOutput = true;            
            p.Start();
            string output = p.StandardOutput.ReadToEnd();
            int len = output.IndexOf("MAC Address = ");
            if(len>0)
            {
                mac = output.Substring(len + 14, 17);
            }            
            p.WaitForExit();            
            return mac;
        }
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取本地IP地址MAC地址,您可以使用C#的System.Net.NetworkInformation命名空间中的NetworkInterface类。以下是一个示例代码: ``` using System.Net.NetworkInformation; // 获取本地IP地址 foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { if (nic.OperationalStatus == OperationalStatus.Up) { IPInterfaceProperties ipProps = nic.GetIPProperties(); foreach (var ip in ipProps.UnicastAddresses) { if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { string ipAddress = ip.Address.ToString(); // 在此处使用WebSocket向服务器发送在线状态注册 } } } } // 获取本地MAC地址 foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { if (nic.OperationalStatus == OperationalStatus.Up) { PhysicalAddress mac = nic.GetPhysicalAddress(); string macAddress = BitConverter.ToString(mac.GetAddressBytes()); // 在此处使用WebSocket向服务器发送在线状态注册 } } ``` 要使用WebSocket向服务器发送在线状态注册,您需要使用C#中的WebSocket类,它位于System.Net.WebSockets命名空间中。以下是一个示例代码: ``` using System.Net.WebSockets; using System.Text; using System.Threading; using System.Threading.Tasks; public static async Task SendWebSocketMessage(string message) { using (ClientWebSocket ws = new ClientWebSocket()) { await ws.ConnectAsync(new Uri("ws://your-server-url"), CancellationToken.None); byte[] messageBytes = Encoding.UTF8.GetBytes(message); ArraySegment<byte> messageSegment = new ArraySegment<byte>(messageBytes); await ws.SendAsync(messageSegment, WebSocketMessageType.Text, true, CancellationToken.None); } } ``` 将上述代码与获取本地IP地址MAC地址的代码结合起来,您可以编写一个方法来向服务器发送在线状态注册: ``` public static async Task RegisterOnlineStatus() { // 获取本地IP地址 foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { if (nic.OperationalStatus == OperationalStatus.Up) { IPInterfaceProperties ipProps = nic.GetIPProperties(); foreach (var ip in ipProps.UnicastAddresses) { if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { string ipAddress = ip.Address.ToString(); // 向服务器发送在线状态注册 string message = $"{{\"type\": \"register\", \"ip\": \"{ipAddress}\"}}"; await SendWebSocketMessage(message); } } } } // 获取本地MAC地址 foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { if (nic.OperationalStatus == OperationalStatus.Up) { PhysicalAddress mac = nic.GetPhysicalAddress(); string macAddress = BitConverter.ToString(mac.GetAddressBytes()); // 向服务器发送在线状态注册 string message = $"{{\"type\": \"register\", \"mac\": \"{macAddress}\"}}"; await SendWebSocketMessage(message); } } } ``` 请注意,此示例代码仅供参考,并且需要根据您的实际情况进行修改。例如,您需要替换“your-server-url”为您的服务器URL。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值