C++:组播代码实现

以下为C++:组播代码实现

multicast_recver.cpp

// it's a demo to receive multicast udp data with the one of multi net interfaces....

#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;

#define MULTICAST_IP         "224.0.1.135"

#define MULTICAST_PORT   60000

int main(void)
{
    char recmsg[256];

    int n;
    int socket_fd;
    struct sockaddr_in group_addr;      //multicast address
    struct sockaddr_in local_addr;        //local address
    struct ip_mreq mreq;
    socklen_t addr_len = sizeof(group_addr);
    u_int yes;

    socket_fd=socket(AF_INET,SOCK_DGRAM,0);
    if(socket_fd < 0)
    {
        perror("socket multicast!");
        exit(1);
    }

    /*set up the local address*/
    memset(&local_addr,0,sizeof(local_addr));
    local_addr.sin_family = AF_INET;

    // 设定本地监听必须是0.0.0.0 (htonl(INADDR_ANY))   这里是关键!
    local_addr.sin_addr.s_addr = inet_addr("0.0.0.0");  

    //this port must be the multicast port
    local_addr.sin_port = htons(MULTICAST_PORT);  

    /*set reuse*/
    if (setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
    {
        perror("Reusing ADDR failed");
        exit(1);
    }

    /*bind local address*/
    if(bind(socket_fd,(struct sockaddr *)&local_addr,sizeof(local_addr)) == -1)
    {
        perror("Binding the multicast!");
        exit(1);
    }
    

    //john in multicast
    mreq.imr_multiaddr.s_addr=inet_addr(MULTICAST_IP);
    //mreq.imr_interface.s_addr=htonl(INADDR_ANY);

    // 设定用本地新网卡地址来接收组播. 这里是关键!
    mreq.imr_interface.s_addr=inet_addr("180.0.0.109");
    if (setsockopt(socket_fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)) < 0)
    {   
        perror("setsockopt multicast!");
        exit(1);
    }

    /*set up the destination address*/
    memset(&group_addr,0,sizeof(struct sockaddr_in));
    group_addr.sin_family = AF_INET;
    group_addr.sin_port = htons(MULTICAST_PORT);
    group_addr.sin_addr.s_addr = inet_addr(MULTICAST_IP);

    /*loop to send or recieve*/
    while(1)
    {

       bzero(recmsg, sizeof(recmsg)-1);
       n_len = recvfrom(socket_fd, recmsg, sizeof(recmsg)-1, 0,
               (struct sockaddr *) &group_addr, &addr_len);
       if (n_len < 0) {
           printf("recvfrom err in udp talk!\n");
           exit(4);
       } else {
           /* success recieve the information */
           recmsg[n] = 0;
           printf("peer:%s \n", recmsg);
       }

    }

    return 0;
}

multicast_sender.cpp

#include

#include

#include

#include

#include

using namespace std;

const int BUF_SIZE = 1024;

#define MULTICAST_IP         "224.0.1.135"

#define MULTICAST_PORT   60000

int main()

{

    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);

    if (sockfd == -1) {

        perror("socket creation failed");

        return 1;

    }

    struct sockaddr_in addr;

    memset(&addr, 0, sizeof(addr));

    addr.sin_family = AF_INET;

    addr.sin_addr.s_addr = inet_addr(MULTICAST_IP);

    addr.sin_port = htons(MULTICAST_PORT);

    string message = "Hello, multicast!";

    if (sendto(sockfd, message.c_str(), message.length(), 0, (struct sockaddr *)&addr, sizeof(addr)) == -1)

{

        perror("sendto failed");

        close(sockfd);

        return 1;

    }

    else

    {

        cout << "Message sent successfully to " << multicast_ip << ":" << multicast_port <<      endl;

    }

    close(sockfd);

    return 0;

}

扩展

Linux:无法接收组播数据

你可能感兴趣的:(#,C++,c++,组播)