You guys can help me.

Source: Internet
Author: User
You can take a look at it for me-Linux general technology-Linux programming and kernel information. The following is a detailed description. It's okay to compile with gcc, but the results are incorrect during running. Let me see what went wrong.

# 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) // create a 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;
}
}

[ This post was last edited by liuke19851220]
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.