1. 程式人生 > >linux獲取本機ip

linux獲取本機ip

int get_ip ( in_addr_t addrs[], int asize  )
{
        int MAXINTERFACES=16;
        int i = 0;
        int fd, intrface = 0;
        struct ifreq buf[MAXINTERFACES]; ///if.h
        struct ifconf ifc; ///if.h
        in_addr_t loopback;
        if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) //socket.h
                return -1; 
        ifc.ifc_len = sizeof buf;
        ifc.ifc_buf = (caddr_t) buf;
        if (ioctl (fd, SIOCGIFCONF, (char *) &ifc) < 0) //ioctl.h
                return -1; 
        intrface = ifc.ifc_len / sizeof (struct ifreq);
 
        loopback = inet_addr("127.0.0.1");
        for (int j = 0; j < intrface && i < asize; ++j)
        {   
                if (ioctl (fd, SIOCGIFADDR, (char *) &buf[j]) < 0)
                {   
                        continue;
                }   
 
                struct sockaddr addr = buf[j].ifr_addr;
 
 
                if (loopback ==
                                ((sockaddr_in*)&addr)->sin_addr.s_addr)
                {   
                        continue;
                }   
 
                addrs[i] = ((sockaddr_in*)&addr)->sin_addr.s_addr;//types
                ++i;
 
        }   
        close (fd);
        return i;

相關推薦

Linux 獲取IP、MAC地址用法大全

getifaddrs()和struct ifaddrs的使用,獲取本機IP     ifaddrs結構體定義如下: struct ifaddrs { struct ifaddrs *ifa_next; /* Next item in

linux獲取ip

int get_ip ( in_addr_t addrs[], int asize  ) {         int MAXINTERFACES=16;         int i = 0;         int fd, intrface = 0;         str

linux獲取IP地址

 ifaddrs結構體定義如下: C程式碼   struct ifaddrs    {        struct ifaddrs  *ifa_next;    /* Next item in list */        char            *ifa_name;    /* Nam

linux 獲取MAC/IP地址的方法

linunx 編程 mac 地址功能:查詢本機IP/MAC地址,過濾掉127.0.0.1 loop-back 地址適用:linux, ubuntu 16.04 調試通過#include <stdlib.h>#include <stdio.h>#include <unistd.h&

獲取IP(適用於Linux系統)

tex cat 獲取本機 smo cal network enum print inet6 獲取本機IP(適用於Linux系統) /** * @desc 獲取本機IP(適用於Linux系統) * @return Ip */ public static String getL

Linux下C語言獲取IP地址

#include <sys/ioctl.h> #include <net/if.h> #include <arpa/inet.h>   char* GetLocalIp() { int MA

linux c 獲取ip地址

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <

Linux下靠譜的獲取IP的C程式碼實

這裡寫程式碼片http://blog.csdn.net/langeldep/article/details/8306603 正在做一個網路程式設計的任務,多臺裝置向伺服器傳送圖片,傳送圖片的時候同時告訴伺服器自己的IP。伺服器根據IP來區分不同的裝置,將圖片存

linux程式設計獲取IP地址的三種方法

                這是一項不太清晰而且沒有多大意義的工作。一個原因是網路地址的設定非常靈活而且都是允許使用者進行個性

Linux下Java獲取IP地址

在Linux下用InetAddress.getLocalHost()方法獲取本機IP地址, 得到的結果總是:127.0.0.1。 原來這個是etc/hosts檔案中的配置,並非網絡卡的IP地址。 後來多方尋訪,終於得下以下程式碼, 執行後在控制檯輸出IP與MAC地址。import java.net.*;imp

Linux下靠譜的獲取IP地址的C程式碼實現

#include <stdio.h> #include <sys/types.h> #include <ifaddrs.h> #include <netinet/in.h> #include <string

java獲取IP (相容linux

程式在本地沒問題,釋出到linux上報錯,百度了一下是ip獲取方法InetAddress.getLocalHost().getHostAddress()不相容linux,網上提供一種解決方案是修改linux伺服器配置,我試了一下,沒用,於是採用另一種方案,

Linux下C程式設計實現---獲取IP地址

在LINUX下,如果需要獲取本機的IP地址,需要用到以下一個結構體,該結構體在/usr/include/net/if.h檔案中 struct ifreq { # define IFHWADDRLEN 6 # define IFNAMSIZ IF_

獲取IP地址的小腳

ash class p地址 nbsp 地址 int done 獲取本機 onf 獲取本機私網地址(1個) 1 #!/bin/bash 2 # Author : standby 3 # Date : 2017-05-18 4 # D

unity 獲取ip地址

ipa mil work get hostname [] 本機 取ip gethost IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName()); //Dns.GetHostName()獲取本機名Dns.GetH

qt獲取ip

獲取本機ip host == spa 獲取本機ip地址 contains proto post bstr //獲取本機IP QString getIP(QString localHost) { QString ipAddr; #if 0

獲取IP地址

down pos -m 兩個 div mar inter 獲取本機ip地址 work 問題描述 一臺電腦有IPV4地址和IPV6地址,目前使用的基本是IPV4地址, 有些情況還有多個網卡,有線的、無線的、還有vmare虛擬的兩個網卡。 獲取所有的IP地址 //獲取本機IP

獲取IP及在IP的基礎上自增1(只針對有一個IP的機器)

turn else data 返回 主機名 add cleanup done get 1、獲取本機IP 1 char* getLocalIP() 2 { 3 WSADATA wsaData; 4 int err = WSAStartup(MAKEW

windows下用C++獲取IP地址

ali 返回 轉換成 data info AC ctrl nag != BSTR CamUtilsCtrl::GET_TERM_IP(void){ AFX_MANAGE_STATE(AfxGetStaticModuleState()); CString strResult

獲取IP

AD new pad address cal 獲取本機 etl foreach 形式 public static String GetLocalIp() {   String[] Ips = GetLocalIpAddress();