获取本机IP

///////////////////////////////获取本机IP

#define MAXADDRS    32

extern char *if_names[MAXADDRS];

extern char *ip_names[MAXADDRS];

extern char *hw_addrs[MAXADDRS];

externunsignedlong
ip_addrs[MAXADDRS];

// Function prototypes

void InitAddresses();

void FreeAddresses();

void GetIPAddresses();

void GetHWAddresses();

/*

*  IPAddress.c

*

*/

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <sys/ioctl.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>

#include <arpa/inet.h>

#include <sys/sockio.h>

#include <net/if.h>

#include <errno.h>

#include <net/if_dl.h>

#define    min(a,b)    ((a) < (b) ? (a) : (b))

#define    max(a,b)    ((a) > (b) ? (a) : (b))

#define BUFFERSIZE    4000

char *if_names[MAXADDRS];

char *ip_names[MAXADDRS];

char *hw_addrs[MAXADDRS];

unsigned long ip_addrs[MAXADDRS];

static int   nextAddr = 0;

void InitAddresses()

{

int i;

for (i=0; i<MAXADDRS;
++i)

{

if_names[i] =
ip_names[i] = hw_addrs[i] = NULL;

ip_addrs[i] = 0;

}

}

void FreeAddresses()

{

int i;

for (i=0; i<MAXADDRS;
++i)

{

if (if_names[i] !=
0) free(if_names[i]);

if (ip_names[i] !=
0) free(ip_names[i]);

if (hw_addrs[i] !=
0) free(hw_addrs[i]);

ip_addrs[i] = 0;

}

InitAddresses();

}

void GetIPAddresses()

{

int            
    i, len, flags;

char           
    buffer[BUFFERSIZE], *ptr,
lastname[IFNAMSIZ], *cptr;

struct ifconf      
ifc;

struct ifreq     
  *ifr, ifrcopy;

struct sockaddr_in   
*sin;

char temp[80];

int sockfd;

for (i=0; i<MAXADDRS;
++i)

{

if_names[i] =
ip_names[i] = NULL;

ip_addrs[i] = 0;

}

sockfd = socket(AF_INET,
SOCK_DGRAM, 0);

if (sockfd < 0)

{

perror("socket failed");

return;

}

ifc.ifc_len = BUFFERSIZE;

ifc.ifc_buf = buffer;

if (ioctl(sockfd,
SIOCGIFCONF, &ifc) < 0)

{

perror("ioctl error");

return;

}

lastname[0] = 0;

for (ptr = buffer; ptr < buffer +
ifc.ifc_len; )

{

ifr = (struct ifreq
*)ptr;

len =
max(sizeof(struct sockaddr),
ifr->ifr_addr.sa_len);

ptr +=
sizeof(ifr->ifr_name) + len;    //
for next one in buffer

if
(ifr->ifr_addr.sa_family !=
AF_INET)

{

continue;    // ignore if not desired
address family

}

if ((cptr = (char
*)strchr(ifr->ifr_name, ‘:‘)) !=
NULL)

{

*cptr = 0; 
      // replace colon will null

}

if (strncmp(lastname,
ifr->ifr_name, IFNAMSIZ) == 0)

{

continue;    /* already processed
this interface */

}

memcpy(lastname,
ifr->ifr_name, IFNAMSIZ);

ifrcopy = *ifr;

ioctl(sockfd,
SIOCGIFFLAGS, &ifrcopy);

flags = ifrcopy.ifr_flags;

if ((flags &
IFF_UP) == 0)

{

continue;    // ignore if interface
not up

}

if_names[nextAddr] =
(char
*)malloc(strlen(ifr->ifr_name)+1);

if
(if_names[nextAddr] == NULL)

{

return;

}

strcpy(if_names[nextAddr],
ifr->ifr_name);

sin = (struct
sockaddr_in *)&ifr->ifr_addr;

strcpy(temp,
inet_ntoa(sin->sin_addr));

ip_names[nextAddr] =
(char
*)malloc(strlen(temp)+1);

if
(ip_names[nextAddr] == NULL)

{

return;

}

strcpy(ip_names[nextAddr], temp);

ip_addrs[nextAddr] =
sin->sin_addr.s_addr;

++nextAddr;

}

close(sockfd);

}

void GetHWAddresses()

{

struct ifconf ifc;

struct ifreq *ifr;

int i, sockfd;

char buffer[BUFFERSIZE], *cp,
*cplim;

char temp[80];

for (i=0; i<MAXADDRS;
++i)

{

hw_addrs[i] = NULL;

}

sockfd = socket(AF_INET,
SOCK_DGRAM, 0);

if (sockfd < 0)

{

perror("socket failed");

return;

}

ifc.ifc_len = BUFFERSIZE;

ifc.ifc_buf = buffer;

if (ioctl(sockfd,
SIOCGIFCONF, (char *)&ifc) <
0)

{

perror("ioctl error");

close(sockfd);

return;

}

ifr = ifc.ifc_req;

cplim = buffer + ifc.ifc_len;

for (cp=buffer; cp < cplim; )

{

ifr = (struct ifreq
*)cp;

if
(ifr->ifr_addr.sa_family ==
AF_LINK)

{

struct
sockaddr_dl *sdl = (struct sockaddr_dl
*)&ifr->ifr_addr;

int a,b,c,d,e,f;

int i;

strcpy(temp,
(char *)ether_ntoa(LLADDR(sdl)));

sscanf(temp,
"%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e,
&f);

sprintf(temp,
"%02X:%02X:%02X:%02X:%02X:%02X",a,b,c,d,e,f);

for (i=0;
i<MAXADDRS; ++i)

{

if
((if_names[i] != NULL) &&
(strcmp(ifr->ifr_name,if_names[i]) ==
0))

{

if (hw_addrs[i] == NULL)

{

hw_addrs[i] = (char
*)malloc(strlen(temp)+1);

strcpy(hw_addrs[i], temp);

break;

}

}

}

}

cp +=
sizeof(ifr->ifr_name) +
max(sizeof(ifr->ifr_addr),
ifr->ifr_addr.sa_len);

}

close(sockfd);

}

////////////////////////////////////////////////////////////

- (NSString *)deviceIPAdress {

InitAddresses();

GetIPAddresses();

GetHWAddresses();

return
[NSStringstringWithFormat:@"%s",
ip_names[1]];

}

- (void)viewDidLoad

{

  NSString *phoneIP = [self deviceIPAdress];

  NSLog("ip = %@", phoneIP);

}

获取本机IP,码迷,mamicode.com

时间: 2024-10-17 13:46:02

获取本机IP的相关文章

获取本机IP地址

这里有两种方法: 1 //获取本机IP 2 - (NSString *)localIPAddress 3 { 4 NSString *localIP = nil; 5 struct ifaddrs *addrs; 6 if (getifaddrs(&addrs)==0) { 7 const struct ifaddrs *cursor = addrs; 8 while (cursor != NULL) { 9 if (cursor->ifa_addr->sa_family == AF_

python:使用netifaces模块获取本机IP网关等信息

python获取本机IP有很多种方法,可每种方法都有局限性. 使用netifaces模块获取本机IP网关等信息,需要安装netifaces模块,不管windows还是linux都可以通用. 一.程序: #!/usr/bin/env python2 # -*- coding: utf-8 -*- #实现本地网卡IP #需要安装netifaces模块 def GetNetworkIP():     #获取本地网卡IP地址     import netifaces     #routingGatewa

关于是用dotnet获取本机IP地址+计算机名的方法

印象中在maxscript帮助文档里找到过方法,但是当时没记下来.只能通过dotnet实现了. 如果电脑有无线网卡和本地连接,可能会出现乱码,也问了写dotnet的朋友,提供了一些思路,不过最终还是使用了这个笨办法. fn getIP_PCname = ( cc = (dotnetclass "System.Net.Dns") oo = cc.GetHostAddresses(cc.GetHostName()) for ip = 1 to oo.count do ( getip = f

Java编程实现获取本机IP和计算机名的功能

import java.net.InetAddress; import java.net.UnknownHostException; public class Test { public static void main(String[] args) { try { InetAddress inetAddress = InetAddress.getLocalHost(); System.out.println("本机IP:" + inetAddress.getHostAddress()

java 获取本机ip及mac地址

package com.achun.test; import java.net.Inet4Address;import java.net.Inet6Address;import java.net.InetAddress;import java.net.NetworkInterface;import java.util.Enumeration; public class HelloWorld { public static void main(String[] args) { // TODO Au

Windows phone8 获取本机 IP 地址

可以在 Bing 查找中输入: "my IP address",然后确认这样可以看到自己的 IP. 如果需要通过代码编程来获取本机 IP,可以使用以下代码: 注意:此段代码在 WP7.1 上无法编译通过,NetworkInformation 未定义. 1 public class GetHostIPAddress 2 { 3 public string GetIPAddress() 4 { 5 string strIPAddress = null; 6 List<string&g

java获取本机IP地址

/** * WIFI没打开:ip为127.0.0.1 * 获取本机IP地址字符串 * @return */ public String getWifiIp() { if (!getWifiEnabled()) { return "127.0.0.1"; } WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); String ip = intToIp(i

Linux编程获取本机IP地址

使用函数getifaddrs来枚举网卡IP,其中使用到的结构体如下所示: struct ifaddrs { struct ifaddrs *ifa_next; /* Next item in list */ char *ifa_name; /* Name of interface */ unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */ struct sockaddr *ifa_addr; /* Address of interface *

#获取本机IP地址时排除IPv6类型,只返回IPv4地址的方法

public static string GetLocalIP(){try{string HostName = Dns.GetHostName(); //得到主机名IPHostEntry IpEntry = Dns.GetHostEntry(HostName); for (int i=0; i < IpEntry.AddressList.Length; i++){//从IP地址列表中筛选出IPv4类型的IP地址//AddressFamily.InterNetwork表示此IP为IPv4,//Ad