• C 语言实现 UDP


    广播

    发送广播信息,局域网中的客户端都可以接受该信息

    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main() {
    
        // 1.创建一个通信的socket
        int fd = socket(PF_INET, SOCK_DGRAM, 0);
        if(fd == -1) {
            perror("socket");
            exit(-1);
        }
    
        // 2.设置广播属性
        int op = 1;
        setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &op, sizeof(op));
    
        // 3.创建一个广播的地址
        struct sockaddr_in cliaddr;
        cliaddr.sin_family = AF_INET;
        cliaddr.sin_port = htons(9999);
        inet_pton(AF_INET, "192.168.244.255", &cliaddr.sin_addr.s_addr);    //本机的广播地址使用命令ifconfig查看
    
        // 3.通信
        int num = 0;
        while(1) {
    
            char sendBuf[128];
            sprintf(sendBuf, "hello, client....%d\n", num++);
            // 发送数据
            sendto(fd, sendBuf, strlen(sendBuf) + 1, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
            printf("广播的数据:%s\n", sendBuf);
            sleep(1);
        }
    
        close(fd);
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    客户端接收广播信息,防火墙可能会影响广播的信息发送。

    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main() {
    
        // 1.创建一个通信的socket
        int fd = socket(PF_INET, SOCK_DGRAM, 0);
        if(fd == -1) {
            perror("socket");
            exit(-1);
        }
        #if 1
        int nOptval;
        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,(const void *)&nOptval , sizeof(int)) < 0)//可以运行多个实例,一起接收广播信息
        {
            perror("setsocket error\n");
            return -1;
        }
        #endif
        struct in_addr in;
    
        // 2.客户端绑定本地的IP和端口
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(9999);
        addr.sin_addr.s_addr = INADDR_ANY;          //绑定本机IP
        int ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
        if(ret == -1) {
            perror("bind");
            exit(-1);
        }
    
        // 3.通信
        while(1) {
    
            char buf[128];
            // 接收数据
            int num = recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL);
            printf("server say : %s\n", buf);
    
        }
    
        close(fd);
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    多播

    单播地址标识单个 IP 接口,广播地址标识某个子网的所有 IP 接口,多播地址标识一组 IP 接口。单播和广播是寻址方案的两个极端(要么单个要么全部),多播则意在两者之间提供一种折中方案。多播数据报只应该由对它感兴趣的接口接收,也就是说由运行相应多播会话应用系统的主机上的接口接收。另外,广播一般局限于局域网内使用,而多播则既可以用于局域网,也可以跨广域网使用。

    P多播通信必须依赖于IP多播地址,在IPv4中它的范围从224.0.0.0到239.255.255.255,并被划分为局部链接多播地址、预留多播地址和管理权限多播地址三类:
    在这里插入图片描述

    #include 
    #include 
    #include 
    #include 
    #include 
     
    int main() {
     
        // 1.创建一个通信的socket
        int fd = socket(PF_INET, SOCK_DGRAM, 0);
        if(fd == -1) {
            perror("socket");
            exit(-1);
        }   
     
        // 2.设置多播的属性,设置外出接口
        struct in_addr imr_multiaddr;
        // 初始化多播地址
        inet_pton(AF_INET, "239.0.0.10", &imr_multiaddr.s_addr);
        setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &imr_multiaddr, sizeof(imr_multiaddr));
        
        // 3.初始化客户端的地址信息,在多播中需要表明客户端的信息
        struct sockaddr_in cliaddr;
        cliaddr.sin_family = AF_INET;
        cliaddr.sin_port = htons(9999);
        inet_pton(AF_INET, "239.0.0.10", &cliaddr.sin_addr.s_addr);
     
        // 3.通信
        int num = 0;
        while(1) {
           
            char sendBuf[128];
            sprintf(sendBuf, "hello, client....%d\n", num++);
            // 发送数据
            sendto(fd, sendBuf, strlen(sendBuf) + 1, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
            printf("组播的数据:%s\n", sendBuf);
            sleep(1);
        }
     
        close(fd);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    #include 
    #include 
    #include 
    #include 
    #include 
     
    int main() {
     
        // 1.创建一个通信的socket,使用UDP通信协议
        int fd = socket(PF_INET, SOCK_DGRAM, 0);
        if(fd == -1) {
            perror("socket");
            exit(-1);
        }   
     
        struct in_addr in;
        // 2.客户端绑定本地的IP和端口
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(9999);
        addr.sin_addr.s_addr = INADDR_ANY;
     
        int ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
        if(ret == -1) {
            perror("bind");
            exit(-1);
        }
     
        struct ip_mreq op;
        inet_pton(AF_INET, "239.0.0.10", &op.imr_multiaddr.s_addr);
        op.imr_interface.s_addr = INADDR_ANY;
     
        // 加入到多播组
        setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &op, sizeof(op));
     
        // 3.通信
        while(1) {
            
            char buf[128];
            // 接收数据
            int num = recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL);
            printf("server say : %s\n", buf);
     
        }
     
        close(fd);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
  • 相关阅读:
    TikTok ERP系统,TikTok 批量上架产品工具
    day34 文件上传&黑白盒审计&逻辑&中间件&外部引用
    分布式事务
    AI低代码,或将再次颠覆开发行业
    QT_day1
    flink web-ui提交New Job报错Server Response Message: Internal server error.
    Nginx + KeepAlived高可用负载均衡集群
    集业界最优资源,SAIC AI LAB 2.0技术架构如何将Robotaxi量产变为可能?
    2023计算机毕设选题 python毕业设计如何选题
    Prometheus----3
  • 原文地址:https://blog.csdn.net/Zlb2214/article/details/134398015