怎么用C语言去设置本地IP地址

怎么用C语言去设置本地IP地址,第1张

#include <iostream>

using namespace std;

#include <stdioh>

#include <sys/socketh>

#include <arpa/ineth>

#include <netinet/inh>

#include <errnoh>

#include <stringh>

#include <stdlibh>

void setIPv4(char  ip,char  gw,char  netmask){

char cmd[128];

//network interface

char nwkInf[5]="eth0";

//link down command in Linux

sprintf(cmd,"ip link set %s down",nwkInf);

system(cmd); 

memset(cmd,0x00,64);

//command to set ip address, netmask

sprintf(cmd,"ifconfig %s %s netmask %s",nwkInf,ip,netmask);

system(cmd);  

printf("\ncmd : %s",cmd); fflush(stdout);

memset(cmd,0X00,64);

//command to set gateway

sprintf(cmd,"route add default gw %s %s",gw,nwkInf);

system(cmd); 

memset(cmd,0X00,64);

//link up command

sprintf(cmd,"ip link set %s up",nwkInf);

system(cmd); 

}

int main(){

//calling function to set network settings

setIPv4("19216810267","192168101","2552552550");

return 0;

}

#include "stdioh"

#include "conioh"

main()

{

int i,j;

char ip[20];

char temp[100];

char ch='\0';

FILE fp;

system("ipconfig >d:\\myiptxt");

if ((fp=fopen("d:\\myiptxt","r"))==NULL)

{

printf("the file can not open:\nPress any key to exit:");

getch();

exit(1);

}

for (i=0;i<7;i++)

{fgets(temp,80,fp); /跳过一些行/

/printf("%s\n",temp); /}

fgets(temp,80,fp);

i=0;j=0;

while (temp[i++]!=':')

;

while (temp[i]!='\n')

ip[j++]=temp[i++];

ip[j]=0;

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

fclose(fp);

system("del d:\\myiptxt");

getch();

}

#include <stdioh>

main(){

system("ipconfig >> myIPtxt"); // 简要的

// system("ipconfig /all >> myIPtxt"); // 非常详细的

printf("look file myIPtxt\n");

return 0;

}

用ipconfig 就可以了。

#include <stdioh>

#include <stdlibh>

#include <unistdh>

#include <stringh>

#include <sys/socketh>

#include <netinet/inh>

#include <net/ifh>

#include <netdbh>

#include <arpa/ineth>

#include <sys/ioctlh>

//获取地址

//返回IP地址字符串

int getlocalip(char outip)

{

int i=0;

int sockfd;

struct ifconf ifconf;

char buf = (char)malloc(512);

struct ifreq ifreq;

char ip;

//初始化ifconf

ifconfifc_len = 512;

ifconfifc_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 = ifconfifc_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,"127001", 3)==0) //排除127xxx,继续下一个

{

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;

}

PcrazyC

-

高级经理

六级

的代码少调用了WSAStartup,没有对库进行初始化所以不会成功的只要加上就没问题的了BCHSHR说的库也要加进去才行在CONSOLE模式和APPLICATION模式其实都是可以的

以上就是关于怎么用C语言去设置本地IP地址全部的内容,包括:怎么用C语言去设置本地IP地址、C语言能获取当前IP吗、c语言 求一个获取本机内网ip的小程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9461539.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存