资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言获取ip函数 c语言获取本机ip地址

请问 , 用c语言怎样获取ip地址啊 谢谢了

看你要获得那里的ip地址

晋江ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!

如果是本及机的话,就使用windows的API啊

#include "winsock.h"

WORD wVersionRequested;

WSADATA wsaData;

char name[255];

char* ip;

PHOSTENT hostinfo;

wVersionRequested = MAKEWORD( 2, 0 );

if ( WSAStartup( wVersionRequested, wsaData ) == 0 )

{

if( gethostname ( name, sizeof(name)) == 0)

{

if((hostinfo = gethostbyname(name)) != NULL)

{

ip = inet_ntoa (*(struct in_addr *)*hostinfo-h_addr_list);

}

}

// ip is ready

WSACleanup( );

}

怎么用C语言获取Linux系统的网卡IP地址

#include stdio.h

#include stdlib.h

#include unistd.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地址字符串

int getlocalip(char* outip)

{

int i=0;

int sockfd;

struct ifconf ifconf;

char buf = (char)malloc(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);

//接下来一个一个的获取IP地址

ifreq = (struct ifreq*)buf;

i = ifconf.ifc_len/sizeof(struct ifreq);

char *pos = outip;

int count;

for(count = 0; (count  5  i  0); i--)

{

ip = inet_ntoa(((struct sockaddr_in*)(ifreq-ifr_addr))-sin_addr);

if(strncmp(ip,"127.0.0.1", 3)==0) //排除127.x.x.x,继续下一个

{

ifreq++;

continue;

}else

{

printf("%s\n", ip);

strcpy(pos,ip);

int len = strlen(ip);

pos = '\t';

pos += len+1;

count ++;

ifreq++;

}

}

free(buf);

return 0;

}

//——————————-函数的调用方式————————————-

int main(int argc, char** argv)

{

char ip = {'*'};

if ( getlocalip( ip ) == 0 )

{

printf("本机IP地址是: %s\n", ip );

}

else

{

printf("无法获取本机IP地址 ");

}

return 0;

}

编程:C语言编程取得本机ip地址

取本地地址

可以从“开始”-“程序”-“附件”-“命令提示符”里面输入英文“ipconfig/all”就可得到IP地址了,LINUX我也不太懂了。

如何用C语言获得本机IP地址

struct in_addr addr;

hostent *pHost = ::gethostbyname("localhost");//在此写入你自己电脑主机名字

switch (pHost-h_addrtype) {

case AF_INET:

printf("internet网络地址类型(AF_INET)\n");

break;

case AF_INET6:

printf("internet网络地址类型(AF_INET)\n");

break;

case AF_NETBIOS:

printf("netbios网络地址类型(AF_NETBIOS)\n");

break;

default:

printf("其它地址类型 %d\n", pHost-h_addrtype);

break;

}

printf("\t地址长度: %d(字节)\n", pHost-h_length);

addr.s_addr = *(u_long *) pHost-h_addr_list[0];

printf("\t第一个IP地址为: %s\n", inet_ntoa(addr));


本文题目:c语言获取ip函数 c语言获取本机ip地址
分享地址:http://cdkjz.cn/article/ddesioh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220