Linux论坛's Archiver

《开源》旗舰电子杂志2009年第1期火热下载!

liuke19851220 发表于 2008-4-15 11:55

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){ /* set the timing 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)  //建立socket
{
}
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-4-15 17:36

顶一下,呵呵

rose 发表于 2008-4-25 14:48

是什么问题,出现什么错误?啥都没有说呀

cugbcat 发表于 2008-4-25 14:51

[quote]原帖由 [i]rose[/i] 于 2008-4-25 14:48 发表 [url=http://bbs.linuxpk.com/redirect.php?goto=findpost&pid=48466&ptid=13826][img]http://bbs.linuxpk.com/images/common/back.gif[/img][/url]
是什么问题,出现什么错误?啥都没有说呀 [/quote]
很多人都这样,扔上来一段代码,啥都不说,然后让人们猜。
别人没有回答问题的义务,想别人回答问题就把问题描述清楚。

页: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.