出處:http://mcuos.com/archiver/tid-8228.html
#include <sys/socket.h>
#include <sys/ioctl.h>0f!o+@’M t-y ~
#include <linux/if.h>
#include <string.h>
#include <stdio.h>)]+a s’S2v
P’C&l9P ^ X
O/N.o7G o’X o h
int main(int argc, char**argv) ^9T7J)] _ P1R A
{
int skfd = 0;1H y o%P3l N-O t [
struct ifreq ifr;
z P &y”}1K3T+G r0?
argc–;
argv++;
if(argc == 0)
{
printf(“Please give ethernet interface name, such as “eth0″.n”); y _ [ q I4b |X
return -1;+Z&p b P [.D0U
}
‘U:_ G2^ b u }
skfd = socket(AF_INET, SOCK_DGRAM, 0);
if(skfd < 0) l,R i m8c M j l!O
{ X$ g/E V
printf(“Open socket error!n”); b#I v’l#e A$G H’^
return -1;
}
strcpy(ifr.ifr_name, argv[0]); // ifname ???5[ n”h-A B
w3D2x J k z)j O [/F-k
if(ioctl(skfd, SIOCGIFFLAGS, &ifr) <0 ) @ Z Wy s j)` C p
{ g l }e z q
printf(“IOCTL error!n”);
printf(“Maybe ethernet inferface %s is not valid!”, ifr.ifr_name);
close(skfd); a%l G R3G(A b
return -1;
}
if(ifr.ifr_flags & IFF_RUNNING)
{ f)L)j { J h0`7l
printf(“%s is running :)n”, ifr.ifr_name);
}
else2f W,O!f0@7I#y M$S
{ c h J$? W B
printf(“%s is not running :(n”, ifr.ifr_name); L t C ] j;K#n,I
}
close(skfd);7P G j2 m3_ j
return 0;
}
Y%J’i8Z#A
以上程序在ubuntu中测试没有问题。但是在芯唐uClinux中运行有问题。查找原因是W90N745的MAC驱动程序没有将网络断线状态上报给kernel。 k&C o L Z T B { U
在驱动程序适当的地方加入上报。使用函数 };J A d s:h s8t
netif_carrier_on(dev);
netif_carrier_off(dev);