linux上c语言 获得网络接口的统计值
来源:互联网 发布:知茵女装品牌介绍 编辑:程序博客网 时间:2024/06/07 02:33
这个东东,蛮好玩的。其实就是读取了/proc/net/dev 文件。
struct netdev_stats {
unsigned long long rx_packets_m; /* total packets received */
unsigned long long tx_packets_m; /* total packets transmitted */
unsigned long long rx_bytes_m; /* total bytes received */
unsigned long long tx_bytes_m; /* total bytes transmitted */
unsigned long rx_errors_m; /* bad packets received */
unsigned long tx_errors_m; /* packet transmit problems */
unsigned long rx_dropped_m; /* no space in linux buffers */
unsigned long tx_dropped_m; /* no space available in linux */
unsigned long rx_multicast_m; /* multicast packets received */
unsigned long rx_compressed_m;
unsigned long tx_compressed_m;
unsigned long collisions_m;
/* detailed rx_errors: */
unsigned long rx_length_errors_m;
unsigned long rx_over_errors_m; /* receiver ring buff overflow */
unsigned long rx_crc_errors_m; /* recved pkt with crc error */
unsigned long rx_frame_errors_m; /* recv'd frame alignment error */
unsigned long rx_fifo_errors_m; /* recv'r fifo overrun */
unsigned long rx_missed_errors_m; /* receiver missed packet */
/* detailed tx_errors */
unsigned long tx_aborted_errors_m;
unsigned long tx_carrier_errors_m;
unsigned long tx_fifo_errors_m;
unsigned long tx_heartbeat_errors_m;
unsigned long tx_window_errors_m;
};
static char * get_name(char * name, char * p)
{
char * t = NULL;
/*interface only contains lowercase letters*/
while((*p<'a') || (*p>'z')) p++;
if ((t = strchr(p, ':')))
{
memcpy(name, p, t-p);
return t+1;
}
else return NULL;
}
#define PROC_NET_DEV_FNAME "/proc/net/dev"
int get_devstats(char * ifname, struct netdev_stats * pstats)
{
FILE * fp;
char name[256] = {0};
char buf[256] = {0};
char * s = NULL;
int found = 0;
if (!ifname || !pstats) return -1;
fp = fopen(PROC_NET_DEV_FNAME, "r");
if (!fp) return -1;
else
{
/*by pass the first 2 lines, they are titles*/
fgets(buf, sizeof(buf), fp );
fgets(buf, sizeof(buf), fp );
memset(buf, 0 ,sizeof(buf));
while (fgets(buf, sizeof(buf), fp ))
{
memset(name, 0, sizeof(name));
s = get_name(name, buf);
if (s)
{
sscanf(s, "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu",
&pstats->rx_bytes_m, /* missing for 0 */
&pstats->rx_packets_m,
&pstats->rx_errors_m,
&pstats->rx_dropped_m,
&pstats->rx_fifo_errors_m,
&pstats->rx_frame_errors_m,
&pstats->rx_compressed_m, /* missing for <= 1 */
&pstats->rx_multicast_m, /* missing for <= 1 */
&pstats->tx_bytes_m, /* missing for 0 */
&pstats->tx_packets_m,
&pstats->tx_errors_m,
&pstats->tx_dropped_m,
&pstats->tx_fifo_errors_m,
&pstats->collisions_m,
&pstats->tx_carrier_errors_m,
&pstats->tx_compressed_m /* missing for <= 1 */
);
if(!strncmp(name, ifname, strlen(ifname)))
{
found = 1;
break;
debug("name :%s/n", name);
debug("%llu:%llu:%lu:%lu:%lu:%lu:%lu:%lu:%llu:%llu:%lu:%lu:%lu:%lu:%lu:%lu:/n",
pstats->rx_bytes_m, /* missing for 0 */
pstats->rx_packets_m,
pstats->rx_errors_m,
pstats->rx_dropped_m,
pstats->rx_fifo_errors_m,
pstats->rx_frame_errors_m,
pstats->rx_compressed_m, /* missing for <= 1 */
pstats->rx_multicast_m, /* missing for <= 1 */
pstats->tx_bytes_m, /* missing for 0 */
pstats->tx_packets_m,
pstats->tx_errors_m,
pstats->tx_dropped_m,
pstats->tx_fifo_errors_m,
pstats->collisions_m,
pstats->tx_carrier_errors_m,
pstats->tx_compressed_m /* missing for <= 1 */
);
}
}
else continue;
}
fclose(fp);
}
if (!found) return -1;
else return 0;
}
int main()
{
struct netdev_stats wl_pstats;
get_devstats("wl0", &wl_pstats);
return 0;
}
- linux上c语言 获得网络接口的统计值
- C语言 获得Linux用户名
- linux C语言接口
- linux 用C语言获得CPU的占用率
- linux 用C语言获得CPU的占用率
- Linux C语言获得执行的shell命令的返回值
- Linux下如何用C语言获得网络信息,比如IP,掩码,mac地址,网关
- C语言执行Linux的shell命令并获得返回值
- C语言执行Linux的shell命令并获得返回值
- C语言执行Linux的shell命令并获得返回值
- C语言执行Linux的shell命令并获得返回值
- C语言执行Linux的shell命令并获得返回值
- C语言执行Linux的shell命令并获得返回值
- linux下的c语言/网络/网络编程面试题
- linux下的c语言-网络-网络编程面试题
- linux下的c语言-网络-网络编程面试题
- linux C语言 getpagesize获得页内存大小
- linux C语言 getpagesize获得页内存大小
- vs2005新建项目中没有ASP.NET WEB应用程序的解决方法
- Symbian中多语言的实现
- webwork项目移植到struts2
- 误会
- 站长必备SEO工具
- linux上c语言 获得网络接口的统计值
- 档案丢失社保难办是谁的错?
- 利用ListView和DataPager控件来对数据分页
- java处理 Excel(POI包)
- java DOM 注意事项
- SqlParameter使用Like的问题
- 【转】JavaScript判断浏览器类型及版本
- PE文件格式详解
- 在VC中调用DirectShow全屏播放视频