#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <arpa/inet.h>
#include <errno.h>
#define ETH_NAME "eth0"
unsigned int get_local_ip()
{
int sock;
struct sockaddr_in sin;
struct ifreq ifr;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
perror("socket");
return -1;
}
strcpy((char*)&ifr.ifr_name, "eth0");
if(ioctl(sock, SIOCGIFADDR, &ifr) < 0)
{
perror("ioctl");
return -1;
}
memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
printf("----------->>%s\n", inet_ntoa(sin.sin_addr));
return (*(unsigned int*)&sin.sin_addr);
}
int main()
{
printf("------>>>>0x%x\n", get_local_ip());
return 0;
}
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <arpa/inet.h>
#include <errno.h>
#define ETH_NAME "eth0"
unsigned int get_local_ip()
{
int sock;
struct sockaddr_in sin;
struct ifreq ifr;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
perror("socket");
return -1;
}
strcpy((char*)&ifr.ifr_name, "eth0");
if(ioctl(sock, SIOCGIFADDR, &ifr) < 0)
{
perror("ioctl");
return -1;
}
memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
printf("----------->>%s\n", inet_ntoa(sin.sin_addr));
return (*(unsigned int*)&sin.sin_addr);
}
int main()
{
printf("------>>>>0x%x\n", get_local_ip());
return 0;
}