from:http://blog.csdn.net/wj4064/article/details/2553158
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
-
int main(int argc, char *argv[])
-
{
-
-
char ipbuf[16];
-
getlocaip(ipbuf);
-
-
printf("loca ip = %s/n", ipbuf);
-
exit(0);
-
-
}
-
-
int getlocaip(char *ip)
-
{
-
int sockfd;
-
-
if(-1 == (sockfd = socket(PF_INET, SOCK_STREAM, 0)))
-
{
-
perror( "socket" );
-
return -1;
-
}
-
-
struct ifreq req;
-
struct sockaddr_in *host;
-
bzero(&req, sizeof(struct ifreq));
-
strcpy(req.ifr_name, "eth0");
-
ioctl(sockfd, SIOCGIFADDR, &req); //设置IP
-
host = (struct sockaddr_in*)&req.ifr_addr;
-
strcpy(ip, inet_ntoa(host->sin_addr));
-
close(sockfd);
-
return 1;
-
}
SIOCGSIZIFCONF
|
获得获取 SIOCGIFCONF 返回的所有接口的配置信息所需的内存。
ioctl(fd, SIOCGSIZIFCONF, (caddr_t)&ifconfsize);
int ifconfsize;
|
|
SIOCGIFADDR
SIOCSIFADDR
|
SIOCGIFADDR 获取接口地址,而 SIOCSIFADDR 设置接口地址。ifr.ifr_addr 字段返回地址。
ioctl(fd, SIOCGIFADDR, (caddr_t)&ifr, sizeof(struct ifreq));
ioctl(fd, SIOCSIFADDR, (caddr_t)&ifr, sizeof(struct ifreq));
struct ifreq ifr;
|
|
SIOCGIFCONF
|
返回系统中配置的所有接口的配置信息。
ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc);
struct ifconf ifconf;
|
|
阅读(2287) | 评论(0) | 转发(0) |