分页: 1 / 1

[问题]请教c语言编写的获得网络流量的程序,急!

发表于 : 2008-04-11 21:11
liuke19851220
用c语言编写的简单获得网络流量的程序,如下
#include <netdb.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/if_ether.h>
#include <linux/net.h>
#include <net/ethernet.h>
#include <errno.h>
#include <netpacket/packet.h>
#include <asm/types.h>
#include <endian.h>
#include <byteswap.h>
#include <pthread.h>
#include <sys/un.h>
#include <time.h>
#include <sys/time.h>

void set_timer(int interval){
struct itimerval itv, oldtv;
itv.it_interval.tv_sec = interval;
itv.it_interval.tv_usec = 0;
itv.it_value.tv_sec = interval;
itv.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &itv, &oldtv);
}
void time_handle()
{
int i;

printf("bandwidth is %d\n",count);
count=0;

}



int main()
{
int sock;
struct ifreq ifstruct;
struct sockaddr_ll sll;
struct sockaddr_in addr;
char buf[2000];
int r;
int len;
len = sizeof(addr);
if((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_IPV6))) == -1)
{
}
sll.sll_family = PF_PACKET;
sll.sll_protocol = htons(ETH_P_IPV6);
strcpy(ifstruct.ifr_name, "eth1");
ioctl(sock, SIOCGIFINDEX, &ifstruct);
sll.sll_ifindex = ifstruct.ifr_ifindex;
bind(sock,(struct sockaddr*)&sll,sizeof(struct sockaddr_ll));
signal(SIGALRM, time_handle);
set_timer(1);
count=0;
for(; ;)
{
r = recvfrom(sock,(char *)buf,sizeof(buf), 0, (struct sockaddr *)&addr,&len);
count+=r;
}
}
编译的时候出了问题,运行时得到的不是希望的结果,希望大哥大姐们能帮忙指出,哪里错了

发表于 : 2008-04-11 21:57
eexpress
找一个符合条件的源码看看吧。
不就apt-get source xxx就有了。
显示流量的软件,有conky gkrellm这些。