当前位置: 技术问答>linux和unix
如何获得本机IP地址?
来源: 互联网 发布时间:2014-12-30
本文导语: 请指教!谢谢 | #include #include #include #include #include #include #include #include #include #define ETH_NAME "eth0" int main() { int sock; struct sockaddr_in sin; struct ifreq ifr; sock = socket(AF_INET, SOC...
请指教!谢谢
|
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ETH_NAME "eth0"
int main()
{
int sock;
struct sockaddr_in sin;
struct ifreq ifr;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
perror("socket");
return -1;
}
strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
if (ioctl(sock, SIOCGIFADDR, &ifr)
#include
#include
#include
#include
#include
#include
#include
#include
#define ETH_NAME "eth0"
int main()
{
int sock;
struct sockaddr_in sin;
struct ifreq ifr;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
perror("socket");
return -1;
}
strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
if (ioctl(sock, SIOCGIFADDR, &ifr)