欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用C#编写获取远程IP,MAC的方法

程序员文章站 2023-11-09 12:50:22
如果要想获得远程的地址,需要用sendarp这个函数来实现。具体的代码如下:  [dllimport("iphlpapi.dll")]  private...
如果要想获得远程的地址,需要用sendarp这个函数来实现。具体的代码如下: 
[dllimport("iphlpapi.dll")] 
private static unsafe extern int sendarp(int32 dest,int32 host,ref intptr mac,ref intptr length); 
[dllimport("ws2_32.dll")] 
private static extern int32 inet_addr(string ip); 

int32 ldest= inet_addr("157.60.68.163");//目的地的ip 
int32 lhost= inet_addr("157.60.68.33");//本地的ip 

try 

byte[] macinfo=new byte[6]; 
int32 length=6; 

intptr mac=new intptr(macinfo[0]); 
intptr len=new intptr(6); 
int ii=sendarp(ldest,lhost, ref mac, ref len); 

console.writeline("mac add:"+mac); 
console.writeline("length:"+len); 



catch(exception err) 

console.writeline(err);