Multicast (multicast) implemented under Linux

Source: Internet
Author: User
Tags htons

Unicast can only be sent to one receiver, but when sent to multiple receivers, it is not only consuming traffic, but also time consuming, total traffic = The receiver of the traffic per recipient.

Broadcast mode is sent to all hosts, the disadvantage of broadcasting is to cause information pollution, a large amount of information will occupy the network bandwidth.

Multicast (multicast): Only hosts that join a multicast group can receive data. Multicasting can be sent to multiple hosts and avoids excessive loads like broadcasts.

Multicast address is Class D address: 224.0.1.1-239.255.255.255

Then the code is implemented as follows:

Group Advertisement Send

1#include <sys/socket.h>2#include <sys/types.h>3#include <arpa/inet.h>4#include <netinet/inch.h>5#include <stdio.h>6#include <stdlib.h>7#include <errno.h>8#include <error.h>9#include <string.h>Ten#include <unistd.h> One#include <time.h> A  - #defineMcast_port 50001 - #defineMcast_addr "239.0.0.1" the  - #defineError_exit (_errmsg_) error (Exit_failure, errno, _errmsg_) -  - #defineBuff_size 1024 +  - intMain () + { A     intsockfd; at     structsockaddr_in mcastaddr; -     Char*buff =NULL; -     intnbytes; - time_t time_sec; -      -     /** Create user packet sockets **/ in     if(-1= = (SOCKFD = socket (af_inet, SOCK_DGRAM,0)))  -Error_exit ("Socket"); to          +     /** Specify the recipient address as the multicast address **/ -mcastaddr.sin_family =af_inet; theMcastaddr.sin_port =htons (mcast_port); *MCASTADDR.SIN_ADDR.S_ADDR =inet_addr (mcast_addr); $     Panax Notoginseng     /** Connect to the multicast address **/ -     if(-1= = Connect (SOCKFD, (structSOCKADDR *) &mcastaddr,sizeof(MCASTADDR))) theError_exit ("Bind"); +  ATime (&time_sec); the      while(2)  { +Sleep1); -Time_sec + +; $Buff = CTime (&time_sec); $printf"%s", buff); -         /** Data transmission **/ -         if(-1= = Send (SOCKFD, buff, strlen (buff),0)) theError_exit ("Send"); -     }Wuyi Close (SOCKFD); the  -     return 0; Wu}

Multicast receive, with multiple receivers

1#include <stdio.h>2#include <stdlib.h>3#include <errno.h>4#include <sys/types.h>5#include <sys/socket.h>6#include <netinet/inch.h>7#include <arpa/inet.h>8#include <unistd.h>9#include <string.h>Ten#include <strings.h> One  A #defineN 128 -  -typedefstructsockaddr SA; the  - intMainintargcConst Char*argv[]) - { -     intsockfd; +      -     /** Create user packet sockets **/ +     if(SOCKFD = socket (af_inet, SOCK_DGRAM,0)) == -1) A     { atPerror ("Socket"); -Exit (-1); -     } -          -     structsockaddr_in myaddr; -myaddr.sin_family =af_inet; inMYADDR.SIN_ADDR.S_ADDR = inet_addr ("239.0.0.1"); -Myaddr.sin_port = htons (50001); to      +     /** Bind multicast ip**/ -     if(Bind (SOCKFD, (structSOCKADDR *) &myaddr,sizeof(MYADDR)) == -1) the     { *Perror ("Bind"); $Exit (-1);Panax Notoginseng     } -      the     /* + * * struct Ip_mreq { A * * struct in_addr imr_multiaddr; IP Multicast address of group set up multicast group addresses the * * struct in_addr imr_interface; Local IP address of interface native IP +     ** }; -     */ $     /** Join a multicast group **/ $     structip_mreq mreq; -Bzero (&mreq,sizeof(Mreq)); -MREQ.IMR_MULTIADDR.S_ADDR = inet_addr ("239.0.0.1"); theMREQ.IMR_INTERFACE.S_ADDR = inet_addr (/*"0.0.0.0"*/"192.168.1.24"); -         Wuyi     if(SetSockOpt (SOCKFD, Ipproto_ip, Ip_add_membership, &mreq,sizeof(Mreq)) <0) the     { -Perror ("setsockopt"); WuExit (-1); -     } About  $     CharBuf[n] = {0}; -  -      while(1) -     { A         /** Waiting to receive data **/ +Recvfrom (SOCKFD, buf, N,0, NULL, NULL); theprintf"recv:%s\n", buf); -     } $  the Close (SOCKFD); the     return 0; the}

Multicast (multicast) implemented under Linux

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.