UDP多对多组播通信

admin2024-05-15  0

广播和多播仅应用于UDP。TCP是一个面向连接的协议,TCP一定是点对点的,一点是两个主机来建立连接的,TCP肯定是单播。只有UDP才会使用广播和组播。

如下示例实现一个UDP多对多的组播通信,进程中有收、发两个线程,分别表示往组播发送、接收数据。

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>

// 组播地址必须是D类地址,224.0.0.0~239.255.255.255
#define GROUP_IP "239.0.0.1"
#define GROUP_PORT 8888
#define MAX_MESSAGE 128

void *sender(void *arg) {
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        printf("Sender alloc socket failed! %s", strerror(errno));
        pthread_exit(NULL);
    }
    
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(GROUP_IP);
    addr.sin_port = htons(GROUP_PORT);
    
    while(1) {
        char message[MAX_MESSAGE];
        printf("Enter message to send: ");
        fgets(message, MAX_MESSAGE, stdin);
        sendto(sockfd, message, strlen(message), 0, (struct sockaddr *)&addr, sizeof(addr));
    }
    
    close(sockfd);
    pthread_exit(NULL);
}

void *receiver(void *arg) {
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        printf("Receiver alloc socket failed! %s", strerror(errno));
        pthread_exit(NULL);
    }
    
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(GROUP_IP);
    addr.sin_port = htons(GROUP_PORT);

    struct ip_mreq mreq;
    mreq.imr_multiaddr.s_addr = inet_addr(GROUP_IP);
    mreq.imr_interface.s_addr = htonl(INADDR_ANY);
    
    /* 加入组播 */
    int ret = setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
    if (ret < 0) {
        printf("IP_ADD_MEMBERSHIP failed! %s", strerror(errno));
        pthread_exit(NULL);
    }

    /* 设置端口复用,多个进程监听同一端口 */
    ret =  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &mreq, sizeof(mreq));
    if (ret < 0) {
        printf("SO_REUSEADDR failed! %s", strerror(errno));
        pthread_exit(NULL);
    }

    ret = bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
    if (ret < 0) {
        printf("Bind failed! %s", strerror(errno));
        pthread_exit(NULL);
    }

    char message[MAX_MESSAGE];
    struct sockaddr_in sender_addr;
    socklen_t sender_len = sizeof(sender_addr);
    
    while(1) {
        recvfrom(sockfd, message, MAX_MESSAGE, 0, (struct sockaddr *)&sender_addr, &sender_len);
        printf("Received message from %s:%d - %s\n", inet_ntoa(sender_addr.sin_addr), ntohs(sender_addr.sin_port), message);
    }
    
    close(sockfd);
    pthread_exit(NULL);
}

int main() {
    pthread_t threadA, threadB;
    
    pthread_create(&threadA, NULL, sender, NULL);
    pthread_create(&threadB, NULL, receiver, NULL);
    
    pthread_join(threadA, NULL);
    pthread_join(threadB, NULL);
    
    return 0;
}

运行结果如下:

UDP多对多组播通信,在这里插入图片描述,第1张

使用任意节点发送消息,其它的节点都能收到。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!