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

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

#include <iostream>

using namespace std

#include <stdio.h>

#include <sys/socket.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#include <errno.h>

#include <string.h>

#include <stdlib.h>

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("192.168.10.267","192.168.10.1","255.255.255.0")

return 0

}

#include #include #include #define N 50 typedef struct { char eth[N], ip[N], netmasks[20], gw[N], dns[N], tag[N]}dataint main() { data list[]= { { "本地连接", "222.22.65.173", //IP地址 "255.255.255.128",//


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

原文地址: http://outofmemory.cn/tougao/6600438.html

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

发表评论

登录后才能评论

评论列表(0条)

保存