Linux编程获取本地IP

 #include <stdio.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h> //获取本地IP地址
char *getIP()
{
int sock_get_ip;
char ipaddr[]; struct sockaddr_in *sin;
struct ifreq ifr_ip; if ((sock_get_ip=socket(AF_INET, SOCK_STREAM, )) == -)
{
printf("socket create failse...GetLocalIp!/n");
return "";
} memset(&ifr_ip, , sizeof(ifr_ip));
strncpy(ifr_ip.ifr_name, "eth0", sizeof(ifr_ip.ifr_name) - ); if( ioctl( sock_get_ip, SIOCGIFADDR, &ifr_ip) < )
{
return "";
}
sin = (struct sockaddr_in *)&ifr_ip.ifr_addr;
strcpy(ipaddr,inet_ntoa(sin->sin_addr)); printf("local ip:%s /n",ipaddr);
close( sock_get_ip ); return ipaddr;
}
上一篇:C#实现DNS解析服务


下一篇:Embedded binary is not signed with the same certificate as the parent app