Linux Multicast implementation

Source: Internet
Author: User
Tags htons

Server

#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <net/if.h> #define Server_port    8000#define client_port 9000#define MAXLINE 1500#define GROUP "239.0.0.2" int main (void) {int sockfd;    struct sockaddr_in serveraddr, clientaddr;    Char Buf[maxline] = "xingwenpeng\n";    struct IP_MREQN Group;    /* construct socket for UDP communication */SOCKFD = socket (af_inet, SOCK_DGRAM, 0);    Bzero (&serveraddr, sizeof (SERVERADDR));            serveraddr.sin_family = af_inet;         /* IPV4 */serveraddr.sin_addr.s_addr = htonl (Inaddr_any);    /* Local any IP inaddr_any = 0 */serveraddr.sin_port = htons (Server_port);    Bind (SOCKFD, (struct sockaddr *) &serveraddr, sizeof (SERVERADDR));    /* Set the group address */Inet_pton (af_inet, Group, &AMP;GROUP.IMR_MULTIADDR);    /* Local arbitrary ip*/Inet_pton (af_inet, "0.0.0.0", &group.imr_address); /* eth0--Number * command: IP ad */Group.imr_ifindex = If_nametoindex ("eth0");    SetSockOpt (SOCKFD, Ipproto_ip, ip_multicast_if, &group, sizeof (group));    /* Construct client Address ip+ port */bzero (&clientaddr, sizeof (CLIENTADDR));    clientaddr.sin_family = af_inet;    /* IPV4 239.0.0.2+9000 */Inet_pton (af_inet, GROUP, &clientaddr.sin_addr.s_addr);    Clientaddr.sin_port = htons (Client_port);    int i = 0;        while (1) {sprintf (buf, "Itcast%d\n", i++);        Fgets (buf, sizeof (BUF), stdin);        SendTo (SOCKFD, buf, strlen (BUF), 0, (struct sockaddr *) &clientaddr, sizeof (CLIENTADDR));    Sleep (1);    } close (SOCKFD); return 0;}

Client:

#include <netinet/in.h> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h># Include <arpa/inet.h> #include <string.h> #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <net/if.h> #define SERVER_PORT 8000#define MAXLINE 4096#define    Client_port 9000#define GROUP "239.0.0.2" int main (int argc, char *argv[]) {struct sockaddr_in serveraddr, localaddr;    int CONFD;    ssize_t Len;    Char Buf[maxline];    /* Multicast struct */struct IP_MREQN group;    1. Create a socket CONFD = socket (af_inet, SOCK_DGRAM, 0);    2. Initialize the local-side address bzero (&localaddr, sizeof (LOCALADDR));    localaddr.sin_family = af_inet;    Inet_pton (Af_inet, "0.0.0.0", &localaddr.sin_addr.s_addr);    Localaddr.sin_port = htons (Client_port);    Bind (CONFD, (struct sockaddr *) &localaddr, sizeof (LOCALADDR));    /* Set the group address */Inet_pton (af_inet, Group, &AMP;GROUP.IMR_MULTIADDR); /* Local any ip*/Inet_pton (Af_inet, "0.0.0.0 ", &group.imr_address);    /* eth0--number//command: IP ad */group.imr_ifindex = If_nametoindex ("eth0");    /* Set the client to join the multicast group */setsockopt (CONFD, Ipproto_ip, ip_add_membership, &group, sizeof (group));        while (1) {len = Recvfrom (CONFD, buf, sizeof (BUF), 0, NULL, 0);    Write (Stdout_fileno, buf, Len);    } close (CONFD); return 0;}

  

Linux Multicast implementation

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.