Example C code - Multicast

multicast_client.c

/*
*multicast_client.c - 多播的客戶端
*/
#define MCAST_PORT 8888
#define MCAST_ADDR "224.0.0.88"
#define MCAST_DATA "BROADCAST TEST DATA" /*多播發送的數據*/
#define MCAST_INTERVAL 5 /*發送間隔時間*/

int main(int argc, char* argv)
{
    int s;
    struct sockaddr_in mcast_addr;
    s = socket(AF_INET, SOCK_DGRAM, 0); /*建立套接字*/
    if (s == -1) {
        perror("socket()");
        return -1;
    }

    memset(&mcast_addr, 0, sizeof(mcast_addr)); /*初始化IP多播地址為0*/
    mcast_addr.sin_family = AF_INET; /*設置協議族類行為AF*/
    mcast_addr.sin_addr.s_addr = inet_addr(MCAST_ADDR); /*設置多播IP地址*/
    mcast_addr.sin_port = htons(MCAST_PORT); /*設置多播端口*/

    /*向多播地址發送數據*/
    while (1) {
        int n = sendto(s, /*套接字描述符*/
            MCAST_DATA, /*數據*/
            sizeof(MCAST_DATA), /*長度*/
            0,
            (struct sockaddr*)&mcast_addr,
            sizeof(mcast_addr));
        if (n < 0) {
            perror("sendto()");
            return -2;
        }
        sleep(MCAST_INTERVAL); /*等待一段時間*/
    }
    return 0;
}

multicast_server.c

/*
*multicast_server.c - 多播服務程序
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <net/if.h>

#define MCAST_PORT 8888
#define MCAST_ADDR "224.0.0.88" /*一個局部連接多播地址,路由器不進行轉發*/
#define MCAST_INTERVAL 5 /*發送間隔時間*/
#define BUFF_SIZE 256 /*接收緩衝區大小*/

int main(int argc, char* argv[])
{
    int s; /*套接字文件描述符*/
    struct sockaddr_in local_addr; /*本地地址*/
    int err = -1;

    s = socket(AF_INET, SOCK_DGRAM, 0); /*建立套接字*/
    if (s == -1) {
        perror("socket()");
        return -1;
    }

    /*初始化地址*/
    memset(&local_addr, 0, sizeof(local_addr));
    local_addr.sin_family = AF_INET;
    local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    local_addr.sin_port = htons(MCAST_PORT);

    /*綁定socket*/
    err = bind(s, (struct sockaddr*)&local_addr, sizeof(local_addr));
    if (err < 0) {
        perror("bind()");
        return -2;
    }

    /*設置回環許可*/
    int loop = 1;
    err = setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));
    if (err < 0) {
        perror("setsockopt():IP_MULTICAST_LOOP");
        return -3;
    }

    struct ip_mreq mreq; /*加入廣播組*/
    mreq.imr_multiaddr.s_addr = inet_addr(MCAST_ADDR); /*廣播地址*/
    mreq.imr_interface.s_addr = htonl(INADDR_ANY); /*網絡接口為默認*/
    /*將本機加入廣播組*/
    err = setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
    if (err < 0) {
        perror("setsockopt():IP_ADD_MEMBERSHIP");
        return -4;
    }

    int times = 0;
    int addr_len = 0;
    char buff[BUFF_SIZE];
    int n = 0;
    /*循環接收廣播組的消息,5次後退出*/
    for (times = 0; times < 5; times++) {
        addr_len = sizeof(local_addr);
        memset(buff, 0, BUFF_SIZE); /*清空接收緩衝區*/

        /*接收數據*/
        n = recvfrom(s, buff, BUFF_SIZE, 0, (struct sockaddr*)&local_addr,
            &addr_len);
        if (n == -1) {
            perror("recvfrom()");
        }
        /*打印信息*/
        printf("Recv %dst message from server:%s\n", times, buff);
        sleep(MCAST_INTERVAL);
    }

    /*退出廣播組*/
    err = setsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));

    close(s);
    return 0;
}

  目錄