澳门新葡亰娱乐网站-www.142net-欢迎您

澳门新葡亰娱乐网站是因为你还没有找到一条正确的致富之路,www.142net是将所有的游戏都汇集在一起的官方平台,因为澳门新葡亰娱乐网站这个网站当中有着大量的游戏攻略,托IP定位技术,传达终端直接到达的精准传播方式。

获取本地

来源:http://www.bhtsgq.com 作者:计算机知识 人气:190 发布时间:2019-05-30
摘要:驷比不上舌参照:Java拿到本机MAC地址/IP地址/主机名 行使办法 第三要生成2个可用的套接字描述符,TCP或UDP的都能够,为便利一般接纳UDP套接字描述符 下一场调用ioctl函数获取具备网络接口

驷比不上舌参照:Java拿到本机MAC地址/IP地址/主机名

行使办法

第三要生成2个可用的套接字描述符,TCP或UDP的都能够,为便利一般接纳UDP套接字描述符
下一场调用ioctl函数获取具备网络接口列表(struct ifconf), 网卡越来越多列表数目越来越多
双重调用ioctl函数从3个个互连网接口中获得子项(struct ifreq)进行输出
每获得3个子项(属性)都要调用壹次ioctl函数, 因为在struct ifreq中利用了union结构, 贰遍只好科学再次回到贰性情质

/// <summary>
/// 获取本地IP
/// </summary>
/// <returns></returns>
public string GetIP()
{
IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList[0];
return ipAddr.ToString();
}
/// <summary>
/// 获取mac地址
/// </summary>
/// <returns></returns>
public string getMacAddr_Local()
{
string madAddr = null;
获取本地。ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc2 = mc.GetInstances();
foreach (ManagementObject mo in moc2)
{
if (Convert.ToBoolean(mo["IPEnabled"]) == true)
{
madAddr = mo["MacAddress"].ToString();
madAddr = madAddr.Replace(':', '-');
}
mo.Dispose();
}
return madAddr;
}

/// <summary>
/// 获取本地IP
/// </summary>
/// <returns></returns>
public string GetIP()
{
IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName;
IPAddress ipAddr = ipHost.AddressList[0];
return ipAddr.ToString();
}
/// <summary>
/// 获取mac地址
/// </summary>
/// <returns></returns>
public string getMacAddr_Local()
{
string madAddr = null;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc2 = mc.GetInstances();
foreach (ManagementObject mo in moc2)
{
if (Convert.ToBoolean(mo["IPEnabled"]) == true)
{
madAddr = mo["MacAddress"].ToString();
madAddr = madAddr.Replace;
}
mo.Dispose();
}
return madAddr;
}

做的改观:

接口结构

以下结构摘自net/if.h头文件中,头文件中定义了累累宏来简化结构成员的访问

struct ifreq   {
# define IFHWADDRLEN 6
# define IFNAMSIZ IF_NAMESIZE
    union {
        char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "en0".  */
    } ifr_ifrn;

    union {
        struct sockaddr ifru_addr;
        struct sockaddr ifru_dstaddr;
        struct sockaddr ifru_broadaddr;
        struct sockaddr ifru_netmask;
        struct sockaddr ifru_hwaddr;
        short int ifru_flags;
        int ifru_ivalue;
        int ifru_mtu;
        struct ifmap ifru_map;
        char ifru_slave[IFNAMSIZ]; /* Just fits the size */
        char ifru_newname[IFNAMSIZ];
        __caddr_t ifru_data;
    } ifr_ifru;
};
# define ifr_name ifr_ifrn.ifrn_name /* interface name */
# define ifr_hwaddr ifr_ifru.ifru_hwaddr /* MAC address */
# define ifr_addr ifr_ifru.ifru_addr /* address */
# define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-p lnk */
# define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
# define ifr_netmask ifr_ifru.ifru_netmask /* interface net mask */
# define ifr_flags ifr_ifru.ifru_flags /* flags */
# define ifr_metric ifr_ifru.ifru_ivalue /* metric */
# define ifr_mtu ifr_ifru.ifru_mtu /* mtu */
# define ifr_map ifr_ifru.ifru_map /* device map */
# define ifr_slave ifr_ifru.ifru_slave /* slave device */
# define ifr_data ifr_ifru.ifru_data /* for use by interface */
# define ifr_ifindex ifr_ifru.ifru_ivalue    /* interface index      */
# define ifr_bandwidth ifr_ifru.ifru_ivalue /* link bandwidth */
# define ifr_qlen ifr_ifru.ifru_ivalue /* queue length */
# define ifr_newname ifr_ifru.ifru_newname /* New name */

struct ifconf   {
    int ifc_len; /* Size of buffer.  */
    union {
        __caddr_t ifcu_buf;
        struct ifreq *ifcu_req;
    } ifc_ifcu;
};
# define ifc_buf ifc_ifcu.ifcu_buf /* Buffer address.  */
# define ifc_req ifc_ifcu.ifcu_req /* Array of structures.  */

以下为ioctl在装置或拿到套接字属性时的一对标志

int ioctl(int fd, int request, ... /* void *arg */);

/* Socket configuration controls. */
#define SIOCGIFNAME 0x8910      /* get iface name       */
#define SIOCSIFLINK 0x8911      /* set iface channel        */
#define SIOCGIFCONF 0x8912      /* get iface list       */
#define SIOCGIFFLAGS    0x8913      /* get flags            */
#define SIOCSIFFLAGS    0x8914      /* set flags            */
#define SIOCGIFADDR 0x8915      /* get PA address       */
#define SIOCSIFADDR 0x8916      /* set PA address       */
#define SIOCGIFDSTADDR  0x8917      /* get remote PA address    */
#define SIOCSIFDSTADDR  0x8918      /* set remote PA address    */
#define SIOCGIFBRDADDR  0x8919      /* get broadcast PA address */
#define SIOCSIFBRDADDR  0x891a      /* set broadcast PA address */
#define SIOCGIFNETMASK  0x891b      /* get network PA mask      */
#define SIOCSIFNETMASK  0x891c      /* set network PA mask      */
#define SIOCGIFMETRIC   0x891d      /* get metric           */
#define SIOCSIFMETRIC   0x891e      /* set metric           */
#define SIOCGIFMEM  0x891f      /* get memory address (BSD) */
#define SIOCSIFMEM  0x8920      /* set memory address (BSD) */
#define SIOCGIFMTU  0x8921      /* get MTU size         */
#define SIOCSIFMTU  0x8922      /* set MTU size         */
#define SIOCSIFNAME 0x8923      /* set interface name       */
#define SIOCSIFHWADDR   0x8924      /* set hardware address     */
#define SIOCGIFENCAP    0x8925      /* get/set encapsulations       */
#define SIOCSIFENCAP    0x8926
#define SIOCGIFHWADDR   0x8927      /* Get hardware address     */
#define SIOCGIFSLAVE    0x8929      /* Driver slaving support   */
#define SIOCSIFSLAVE    0x8930
#define SIOCADDMULTI    0x8931      /* Multicast address lists  */
#define SIOCDELMULTI    0x8932
#define SIOCGIFINDEX    0x8933      /* name -> if_index mapping */
#define SIOGIFINDEX SIOCGIFINDEX    /* misprint compatibility :-)   */
#define SIOCSIFPFLAGS   0x8934      /* set/get extended flags set   */
#define SIOCGIFPFLAGS   0x8935
#define SIOCDIFADDR 0x8936      /* delete PA address        */
#define SIOCSIFHWBROADCAST  0x8937  /* set hardware broadcast addr  */
#define SIOCGIFCOUNT    0x8938      /* get number of devices */

#define SIOCGIFBR   0x8940      /* Bridging support     */
#define SIOCSIFBR   0x8941      /* Set bridging options     */

#define SIOCGIFTXQLEN   0x8942      /* Get the tx queue length  */
#define SIOCSIFTXQLEN   0x8943      /* Set the tx queue length  */

本文由澳门新葡亰发布于计算机知识,转载请注明出处:获取本地

关键词: .NET Mac unp-net 地址

上一篇:10安装与激活,CI10安装破解心得

下一篇:没有了

最火资讯