首页 > 其他分享 >UDP通信 广播 组播

UDP通信 广播 组播

时间:2023-05-15 17:58:08浏览次数:28  
标签:UDP 组播 addr int ip 广播 client saddr include

# UDP通信

 

 # server.c

#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    int lfd = socket(AF_INET, SOCK_DGRAM, 0);
    char *ip_buf = "192.168.248.128";
    int ip;
    inet_pton(AF_INET, ip_buf, &ip);

    struct sockaddr_in saddr;
    saddr.sin_addr.s_addr = ip;
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(9999);
    bind(lfd, (struct sockaddr *) &saddr, sizeof(saddr));

    while(1)
    {
    
        char recvBuf[1024];
        struct sockaddr_in client_addr;
        int client_addr_len = sizeof(client_addr);
        
        // 第五个参数为传输参数,用于获取发送数据客户端的信息
        int len = recvfrom(lfd, recvBuf, sizeof(recvBuf), 0, (struct sockaddr *) &client_addr , &client_addr_len);
        if(len == -1)
        {
            perror("recvfrom");
            exit(-1);
        }
        else if(len == 0)
        {
            printf("client close······");
            exit(-1);
        }
        else
        {
            printf("recv info : %s\n", recvBuf);
        }
        char client_ipBuf[1024];
        inet_ntop(AF_INET, &client_addr.sin_addr.s_addr, client_ipBuf, sizeof(client_ipBuf));
        printf("client ip : %s, port : %d\n", client_ipBuf, ntohs(client_addr.sin_port));

        // 给客户端再回个数据
        sendto(lfd, recvBuf, strlen(recvBuf) + 1, 0, (struct sockaddr *) &client_addr, sizeof(client_addr));




    }
    close(lfd);






    return 0;
}

 

# client.c

#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    int lfd = socket(AF_INET, SOCK_DGRAM, 0);
    char *ip_buf = "192.168.248.128";
    int ip;
    inet_pton(AF_INET, ip_buf, &ip);

    struct sockaddr_in saddr;
    saddr.sin_addr.s_addr = ip;
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(9999);
    int num = 0;
    while(1)
    {
    
        char sendBuf[1024];
        sprintf(sendBuf, "hello, i am client %d\n", num++);

        sendto(lfd, sendBuf, strlen(sendBuf) + 1, 0, (struct sockaddr *) &saddr, sizeof(saddr));
        
        char recvBuf[1024];

        // 服务端的地址已经指定,不需要再获取了,置NULL即可        
        int len = recvfrom(lfd, recvBuf, sizeof(recvBuf), 0, NULL, NULL);
        if(len == -1)
        {
            perror("recvfrom");
            exit(-1);
        }
        else if(len == 0)
        {
            printf("server close······");
            exit(-1);
        }
        else
        {
            printf("recv info : %s\n", recvBuf);
        }
        sleep(1);


    }

    close(lfd);





    return 0;
}

 

 

# 广播

 

 

# server.c

#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    int lfd = socket(AF_INET, SOCK_DGRAM, 0);

    // 设置广播属性
    int opt = 1;
    setsockopt(lfd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt));

    // 创建一个广播地址
    char *ip_buf = "192.168.248.255";
    int ip;
    inet_pton(AF_INET, ip_buf, &ip);

    struct sockaddr_in saddr;
    saddr.sin_addr.s_addr = ip;
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(9999);
    int num = 0;
    while(1)
    {
    
        char sendBuf[1024];
        sprintf(sendBuf, "client...... %d\n", num++);

        sendto(lfd, sendBuf, strlen(sendBuf) + 1, 0, (struct sockaddr *) &saddr, sizeof(saddr));
        printf("广播数据:%s\n", sendBuf);
        
        sleep(1);


    }

    close(lfd);





    return 0;
}

 

# client.c

#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    int lfd = socket(AF_INET, SOCK_DGRAM, 0);
    char *ip_buf = "192.168.248.128";
    int ip;
    inet_pton(AF_INET, ip_buf, &ip);

    struct sockaddr_in saddr;
    saddr.sin_addr.s_addr = INADDR_ANY;
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(9999);
    bind(lfd, (struct sockaddr *) &saddr, sizeof(saddr)); // 谁先接收谁bind
    while(1)
    {
    
        char recvBuf[1024];
        struct sockaddr_in client_addr;
        int client_addr_len = sizeof(client_addr);
        
        // 第五个参数为传输参数,用于获取发送数据客户端的信息
        int len = recvfrom(lfd, recvBuf, sizeof(recvBuf), 0, NULL , NULL);
        if(len == -1)
        {
            perror("recvfrom");
            exit(-1);
        }
        else if(len == 0)
        {
            printf("client close······");
            exit(-1);
        }
        else
        {
            printf("recv info : %s\n", recvBuf);
        }
        // char client_ipBuf[1024];
        // inet_ntop(AF_INET, &client_addr.sin_addr.s_addr, client_ipBuf, sizeof(client_ipBuf));
        // printf("client ip : %s, port : %d\n", client_ipBuf, ntohs(client_addr.sin_port));




    }
    close(lfd);






    return 0;
}

 

# 组播

 

 

 # server.c

#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    int lfd = socket(AF_INET, SOCK_DGRAM, 0);

    // 设置多播地址
    struct in_addr opt;
    inet_pton(AF_IENT, "239.0.0.10", &opt.s_addr);

    setsockopt(lfd, IPPROTO_IP, IP_MULTICAST_IF, &opt, sizeof(opt));

    // 创建一个多播地址
    char *ip_buf = "239.0.0.10";
    int ip;
    inet_pton(AF_INET, ip_buf, &ip);

    struct sockaddr_in saddr;
    saddr.sin_addr.s_addr = ip;
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(9999);
    int num = 0;
    while(1)
    {
    
        char sendBuf[1024];
        sprintf(sendBuf, "client...... %d\n", num++);

        sendto(lfd, sendBuf, strlen(sendBuf) + 1, 0, (struct sockaddr *) &saddr, sizeof(saddr));
        printf("广播数据:%s\n", sendBuf);
        
        sleep(1);


    }

    close(lfd);





    return 0;
}

 

# client.c

#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>

int main()
{
    int lfd = socket(AF_INET, SOCK_DGRAM, 0);
    char *ip_buf = "192.168.248.128";
    int ip;
    inet_pton(AF_INET, ip_buf, &ip);

    struct sockaddr_in saddr;
    saddr.sin_addr.s_addr = INADDR_ANY;
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(9999);
    bind(lfd, (struct sockaddr *) &saddr, sizeof(saddr)); // 谁先接收谁bind

    // 加入多播组
    struct ip_mreq opt;
    inet_pton(AF_INET, "239.0.0.10", &opt.imr_multiaddr);
    opt.imr_interface = INADDR_ANY;
    setsockopt(lfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, )
    
    while(1)
    {
    
        char recvBuf[1024];
        struct sockaddr_in client_addr;
        int client_addr_len = sizeof(client_addr);
        
        // 第五个参数为传输参数,用于获取发送数据客户端的信息
        int len = recvfrom(lfd, recvBuf, sizeof(recvBuf), 0, NULL , NULL);
        if(len == -1)
        {
            perror("recvfrom");
            exit(-1);
        }
        else if(len == 0)
        {
            printf("client close······");
            exit(-1);
        }
        else
        {
            printf("recv info : %s\n", recvBuf);
        }
        // char client_ipBuf[1024];
        // inet_ntop(AF_INET, &client_addr.sin_addr.s_addr, client_ipBuf, sizeof(client_ipBuf));
        // printf("client ip : %s, port : %d\n", client_ipBuf, ntohs(client_addr.sin_port));




    }
    close(lfd);






    return 0;
}

 

标签:UDP,组播,addr,int,ip,广播,client,saddr,include
From: https://www.cnblogs.com/WTSRUVF/p/17402658.html

相关文章

  • UDP组播的c++实现
    1写socket的时候UDP和TCP的代码区别就是是否有连接过程;有connect连接的代码的就是TCP,没有连接的就是UDP以下代码是发送信息给组播地址(没有写接收代码。接收的代码就是要写个加入多播组,从多播组接收的逻辑)参考:https://blog.csdn.net/zhizhengguan/article/details/109312144/......
  • UDP内核发包流程
    背景工作中遇到客户反馈,上层应用UDP固定间隔100ms发包,但本地tcpdump抓包存在波动,有的数据包之间间隔107ms甚至更多,以此重新梳理了下udp的发送流程。udp发包流程udp_sendmsgUDPcorking是一项优化技术,允许内核将多次数据累积成单个数据报发送。在用户程序中有两种方法可以启......
  • UDP广播相关知识,chatGPT回答的
    1路由器和交换机哪个成环会引起广播风暴路由器和交换机都可能引起广播风暴,但是在成环的情况下,交换机更容易引起广播风暴。当交换机形成环路时,广播帧会在网络中不断循环,并且不断地复制并增加它们的数量,最终导致网络拥塞甚至崩溃。为了避免这种情况,网络管理员应该采取措施,例如使用S......
  • FFmpeg向组播地址推流
    ffmpeg.exe-re-stream_loop-1-iC:\test_01.mp4-vcodeccopy-pkt_size1400-fh264"udp://239.255.255.250:54546"推一个MP4到本机的组播地址(netshinterfaceipv4showjoins 查看win本机的组播地址) 从这个组播地址拉流,注意不是组播分发出去再拉回来,相当于这里是拉......
  • Python NumPy 广播(Broadcast)
    广播(Broadcast)是numpy对不同形状(shape)的数组进行数值计算的方式,对数组的算术运算通常在相应的元素上进行。如果两个数组a和b形状相同,即满足a.shape==b.shape,那么a*b的结果就是a与b数组对应位相乘。这要求维数相同,且各维度的长度相同。importnumpyasnp"......
  • 阿里云服务器无法使用udp连接,防火墙安全组全开仍然无法连接(已解决)
    我使用的是阿里云的ecs服务器,想使用openvpn的udp连接,但是始终无法连接到,最终确定问题到udp连接失败上,期间使用nc测试,客户端能ping通服务器,但是就是不能使用udp连接到,也换过网络换过软件测试,消息都不能发到服务器今天问阿里的售后工程师,才知道他们的安全组分了很多个区域,设置安全......
  • android有序广播和无序广播的区别
    BroadcastReceiver所对应的广播分两类:普通广播和有序广播。普通广播通过Context.sendBroadcast()方法来发送。它是完全异步的。所有的receivers接收器的执行顺序不确定。  因此,所有的receivers接收器接收broadcast的顺序不确定。这种方式效率更......
  • Python+UDP+Threading
    Python+UDP+Threading近期用pythonsocket使用TCP协议做了一个小型的数据收发服务器,后来由于在实际场景中使用时,出现网络不佳导致出现错误的情况,改成了使用UDP协议重做了一版,总体效果变好了。下面是通用代码,实际使用时在这基础上进行修改即可。#-*-coding:utf-8-*-import......
  • C#使用委托在Socket Udp端口侦听线程内更新主窗口控件显示
    c#开启线程侦听SocketUDP端口,端口接收到网络读卡器的读卡数据后刷新UI界面显示接收数据,解析数据包信息并向读卡器发送显示文字、驱动读卡器播报语音、蜂鸣响声提示、开启继电器开关等操作。  .net提示通过设置:CheckForIllegalCrossThreadCalls=false,可以在子线程内强制更新......
  • PyTorch项目实战05——Tensor的广播机制
    1广播机制介绍矩阵运算,往往只能在两个矩阵维度相同或者相匹配时才能运算。比如加减法需要两个矩阵的维度相同,乘法需要前一个矩阵的列数与后一个矩阵的行数相等。当参与运算的两个维度不同也不匹配的矩阵进行运算时,该机制会对数组进行扩展,使数组的shape属性值一样,这样,就可以进行矢......