惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
B
Blog RSS Feed
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Help Net Security
MyScale Blog
MyScale Blog
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
Martin Fowler
Martin Fowler
Vercel News
Vercel News
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
云风的 BLOG
云风的 BLOG

博客园 - 金鹏

独立的MySQL数据库需要做配置才能在外部访问到。 Visual Studio 2008简体中文试用版(90天)变成永久正式版的两种方法 [转]VisualSVN 1.5.x 破解方法详解 Zend Studio for Eclipse 环境搭建 巧设路由器 实现电信、网通南北互通 微软官方SQL Server 2008正式中文试用版下载 [转]如何循序渐进向DotNet架构师发展 [转]Asp.net中防止用户多次登录的方法 使用AJAX动态加栽JS脚本和CSS样式 [转]几种流行的AJAX框架jQuery,Mootools,Dojo,Ext JS的对比 [转]七年IT奋斗纪实及感悟 IE8的主页使用IE8访问居然也报JS错误 今天向四川汶川地震灾区捐献了100元,表达自己的心意. 绿豆蛙主题乐园 如何避免出现“终端服务器超出了最大允许连接数” 超越 —— 零点乐队 C# 2005 & .NET 3.0 高级编程(第5版) [转]解开最后期限的镣铐 性能问题依旧困扰着VS 2008
扫描局域网内电脑,并获得对应的MAC地址和主机名
金鹏 · 2008-08-29 · via 博客园 - 金鹏

代码如下:

using System;
using System.Net;
using System.Threading;

using System.Runtime.InteropServices;

namespace LocalIP {
  
class LanSearch {

    
/// <summary>
    
/// 取MAC地址
    
/// </summary>
    
/// <param name="DestIP">目标IP</param>
    
/// <param name="SrcIP">源IP</param>
    
/// <param name="pMacAddr">MAC地址</param>
    
/// <param name="PhyAddrLen">MAC地址的长度</param>
    
/// <returns></returns>

    [DllImport("iphlpapi.dll", ExactSpelling = true)]
    
private static unsafe extern int SendARP(int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen);

    
/// <summary>
    
/// 在线程中扫描
    
/// </summary>

    private static void LanSearchThreadMethod() {
      
int i = Convert.ToUInt16(Thread.CurrentThread.Name);
      Console.Write(
".");

      
string strIP = "192.168.1." + i.ToString();

      
//IPHostEntry ip = null;
      IPAddress ip = null;
      
try {
        
//ip = Dns.GetHostEntry(strIP);
        ip = IPAddress.Parse(strIP);
      }

      
catch {
        
//Console.WriteLine("请勿输入非法IP地址");
        return;
      }


      
byte[] b = new byte[6];
      
int len = b.Length;
      
//int r = SendARP(BitConverter.ToInt32(ip.AddressList[0].GetAddressBytes(), 0), 0, b, ref len);
      int r = SendARP(BitConverter.ToInt32(ip.GetAddressBytes(), 0), 0, b, ref len);
      
int num = BitConverter.ToInt32(b, 0);
      
string mac = BitConverter.ToString(b, 06);

      
if (num != 0{//有效MAC
        
//Console.WriteLine("\r\n{0}--{1}--{2}", ip.AddressList[0].ToString(), ip.HostName, mac);
        Console.WriteLine("\r\n{0}--{1}", ip.ToString(), mac);
      }

    }


    
/// <summary>
    
/// 程序主入口
    
/// </summary>
    
/// <param name="args"></param>

    [STAThread]
    
static void Main(string[] args) {
      Thread[] thread 
= new Thread[255];
      ThreadStart threadMethod;

      
for (int i = 0; i < 255; i++{
        threadMethod 
= new ThreadStart(LanSearchThreadMethod);
        thread[i] 
= new Thread(threadMethod);
        thread[i].Name 
= i.ToString();
        thread[i].Start();
        
if (!thread[i].Join(100)) {
          thread[i].Abort();
        }

      }


      Console.WriteLine(
"\r\n按任意键返回");
      Console.ReadLine();
    }


  }

}