zhoujun.g 发表于 2018-6-29 09:33:17

linux、windows断网判断机制

#include <linux/sockios.h>  
#include <sys/socket.h>
  
#include <sys/ioctl.h>
  
#include <linux/if.h>
  
#include <string.h>
  
#include <stdio.h>
  
#include <stdlib.h>
  
#include <unistd.h>
  
#define ETHTOOL_GLINK      0x0000000a /* Get link status (ethtool_value) */
  
typedef enum { IFSTATUS_UP, IFSTATUS_DOWN, IFSTATUS_ERR } interface_status_t;
  
typedef signed int u32;
  
/* for passing single values */
  
struct ethtool_value
  
{
  u32    cmd;
  u32    data;
  
};
  
interface_status_t interface_detect_beat_ethtool(int fd, char *iface)
  
{
  struct ifreq ifr;
  struct ethtool_value edata;

  memset(&ifr, 0,>
  strncpy(ifr.ifr_name, iface,>  edata.cmd = ETHTOOL_GLINK;
  ifr.ifr_data = (caddr_t) &edata;
  if (ioctl(fd, SIOCETHTOOL, &ifr) == -1)
  {
  //       perror("ETHTOOL_GLINK failed ");
  return IFSTATUS_ERR;
  }
  return edata.data ? IFSTATUS_UP : IFSTATUS_DOWN;
  
}
  
int main (int argc, char *argv[])
  
{
  FILE *fp;
  interface_status_t status;
  char buf = {'\0'};
  char hw_name = {'\0'};
  char *token = NULL;
  /* 获取活动网卡名称 */
  int fd_eth;
  int interfaceNum = 0;
  struct ifreq buf_eth;
  struct ifconf ifc_eth;
  struct ifreq ifrcopy_eth;
  if ((fd_eth = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  {
  perror("socket");
  close(fd_eth);
  return -1;
  }

  ifc_eth.ifc_len =>  ifc_eth.ifc_buf = (caddr_t)buf_eth;
  if (!ioctl(fd_eth, SIOCGIFCONF, (char *)&ifc_eth))
  {

  interfaceNum = ifc_eth.ifc_len />  //       printf("interface num = %d\n", interfaceNum);
  while (interfaceNum-- > 0)
  {
  if(strstr(buf_eth.ifr_name,"eth") != NULL)
  {
  printf("device name:@@@@@@@@@@@\n");
  printf("device name: %s\n", buf_eth.ifr_name);
  strncpy(hw_name,buf_eth.ifr_name, strlen(buf_eth.ifr_name));
  }
  }
  }
  
//判断是否插入网线
  int fd;
  if((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  {
  perror("socket ");
  exit(0);
  }
  me/yckj/Desktop/linux上svn的使用.docx'
  status = interface_detect_beat_ethtool(fd, hw_name);
  close(fd);
  switch (status)
  {
  case IFSTATUS_UP:
  printf("%s : link up\n", hw_name);
  break;
  case IFSTATUS_DOWN:
  printf("%s : link up\n", hw_name);
  break;
  default:
  printf("Detect Error\n");
  break;
  }
  return 0;
  
}

hihi88en 发表于 2018-7-24 02:38:56

hihi88en 发表于 2018-7-24 02:40:09

hihi88en 发表于 2018-7-24 02:40:41

帽子 发表于 2018-8-15 14:01:11

这是什么语言写的

zijingy 发表于 2018-9-2 13:18:11

为赚金币回复你
页: [1]
查看完整版本: linux、windows断网判断机制