我的需求只是要判断某种网络是否可用,系统中有局域网eth0和3G拨号链接ppp0,有不同的Socket要创建在各自的网络上,写如下函数,传用网络名就可以判断网络是否可用。
// 检测网络连接
// routeName: 网络连接名称,如ppp0、eth0等
// 返回值: 网络正常返回0,异常返回-1
int CheckNetLink(const char *routeName)
{
register int fd, intrface;
struct ifreq buf[16];
struct ifconf ifc;
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)
{
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t) buf;
if (!ioctl(fd, SIOCGIFCONF, (char *) &ifc))
{
intrface = ifc.ifc_len / sizeof (struct ifreq);
while (intrface-- > 0)
{
if (strcmp(buf[intrface].ifr_name, routeName) == 0)
{
ioctl(fd, SIOCGIFFLAGS, (char *) &buf[intrface]);
if ((buf[intrface].ifr_flags & IFF_UP) && (buf[intrface].ifr_flags & IFF_RUNNING))
{
//printf("\n%s is UP & RUNNING\n", routeName);
return OK;
}
else
{
//printf("\n%s is DOWN\n", routeName);
return ERROR;
}
}
}
//printf("\n%s is not exist\n", routeName);
return ERROR;
}
}
return ERROR;
}
关键词标签:Linux
相关阅读
热门文章
安装红帽子RedHat Linux9.0操作系统教程
Tomcat9.0如何安装_Tomcat9.0环境变量配置方法
多种操作系统NTP客户端配置
Linux操作系统修改IP
人气排行 Linux下获取CPUID、硬盘序列号与MAC地址 dmidecode命令查看内存型号 linux tc实现ip流量限制 安装红帽子RedHat Linux9.0操作系统教程 linux下解压rar文件 lcx.exe、nc.exe、sc.exe入侵中的使用方法 Ubuntu linux 关机、重启、注销 命令 查看linux服务器硬盘IO读写负载
查看所有0条评论>>