#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int count;
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()
{
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, "eth0");
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;
}
}
[ 本帖最后由 liuke19851220 于 2008-4-19 08:12 编辑 ]
gongji110 于 2008-04-15 10:53:29发表:
你这个程序我粗略的看了一下,你这个网络接收最好重新开个线程,不然资源都耗在这里了,可能要出一些问题。
我也是刚学,懂不太多。既然编译没问题,运行出错,据是软错误,比较难找,我再看看。