????????gethostname??gethostbyname???????IP?????????????????????????????????????????????????Щ????????ò????????????????????????SOCKET??????????????????????£?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/types.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
//??????
//????IP????????
//?????0=?????-1=???
int getlocalip(char* outip)
{
int i=0;
int sockfd;
struct ifconf ifconf;
char buf[512];
struct ifreq *ifreq;
char* ip;
//?????ifconf
ifconf.ifc_len = 512;
ifconf.ifc_buf = buf;
if((sockfd = socket(AF_INET?? SOCK_DGRAM?? 0))<0)
{
return -1;
}
ioctl(sockfd?? SIOCGIFCONF?? &ifconf);    //??????н?????
close(sockfd);