c语言实现广播,组播

(一)广播

1、广播发送放

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

#define PORT 6666
#define IP "192.168.124.255"
                                                                                                        
#define ERR_MSG(msg) do{\
    fprintf(stderr, "line:%d\n", __LINE__);\
    fprintf(stderr, "func:%s\n", __func__);\
    perror(msg);\
}while(0);

int main(int argc, const char *argv[])
{
    //创建套接字
    int sfd = socket(AF_INET, SOCK_DGRAM,0);
    if(-1 == sfd){
        ERR_MSG("socket");
        return -1;
    }
    printf("创建套接字成功\n");

    //允许端口被重复复用
    int reuse = 1;
    if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        ERR_MSG("setsockopt");
        return -1;
    }
    printf("允许端口快速重用\n");

    //设置允许套接字广播
    int broadcast = 1;
    if(-1 == setsockopt(sfd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast))){
        ERR_MSG("setsockopt");
        return -1;
    }
    printf("设置允许广播成功\n");

    //填充发送方的地址信息结构体,和bind使用
    //真实的地址信息结构体根据地址族来制定
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);

    char buf[128] = "";

    while(1){
        //发送方发送数据
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = '\0';     //将\n字符替换为\0字符
        if(-1 == sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, sizeof(sin))){
            ERR_MSG("sendto");
            return -1;
        }
        printf("send to [%s:%d] :%s\n", inet_ntoa(sin.sin_addr),\
                ntohs(sin.sin_port), buf);
    }

    //关闭套接字
    close(sfd);
    return 0;
}
                                                                                                        

2、接收方

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

#define PORT 6666                                                                              
#define IP "192.168.124.255"

#define ERR_MSG(msg) do{\
    fprintf(stderr, "line:%d\n", __LINE__);\
    fprintf(stderr, "func:%s\n", __func__);\
    perror(msg);\
}while(0);

int main(int argc, const char *argv[])
{
    //创建套接字
    int sfd = socket(AF_INET, SOCK_DGRAM,0);
    if(-1 == sfd){
        ERR_MSG("socket");
        return -1;
    }
    printf("创建套接字成功\n");


    int res;
    char buf[128] = "";
    struct sockaddr_in sender;   //存储对端的地址信息
    sender.sin_family = AF_INET;
    sender.sin_port = htons(PORT);
    sender.sin_addr.s_addr = inet_addr(IP);
    if(-1 == bind(sfd, (struct sockaddr*)&sender, sizeof(sender))){
        ERR_MSG("bind");
        return -1;
    }
    while(1){
        //接收方收数据
        res = recvfrom(sfd, buf, sizeof(buf), 0, NULL, NULL);
        if(-1 == res){
            ERR_MSG("recvfrom");
            return -1;
        }
        printf("[%s:%d] :%s\n", inet_ntoa(sender.sin_addr), ntohs(sender.sin_port), buf);
        memset(buf, 0, sizeof(buf));
    }

    //关闭套接字
    close(sfd);
    return 0;
}

(二)组播

        1、发送方

 #include <stdio.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <netinet/ip.h>
 #include <sys/types.h>
 #include <arpa/inet.h>
 #include <unistd.h>
 #include <string.h>
 
 #define PORT 6666
 #define IP "224.1.2.3"                                                                                   
 
 #define ERR_MSG(msg) do{\
     fprintf(stderr, "line:%d\n", __LINE__);\
     fprintf(stderr, "func:%s\n", __func__);\
     perror(msg);\
 }while(0);
 
 int main(int argc, const char *argv[])
 {
     //创建套接字
     int sfd = socket(AF_INET, SOCK_DGRAM,0);
     if(-1 == sfd){
         ERR_MSG("socket");
         return -1;
     }
     printf("创建套接字成功\n");
     //填充服务器的地址信息结构体,和bind使用
     //真实的地址信息结构体根据地址族来制定
     struct sockaddr_in sin;
     sin.sin_family = AF_INET;
     sin.sin_port = htons(PORT);
     sin.sin_addr.s_addr = inet_addr(IP);
     char buf[128] = "";
     while(1){
         fgets(buf, sizeof(buf), stdin);
         buf[strlen(buf)-1] = '\0';     //将\n字符替换为\0字符
         if(-1 == sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, sizeof(sin))){
             ERR_MSG("sendto");
             return -1;
         }
         printf("send to [%s:%d] :%s\n", inet_ntoa(sin.sin_addr),\
                 ntohs(sin.sin_port), buf);
 
     }
 
     //关闭套接字
     close(sfd);
     return 0;
 }
                                                                                                          
                                                                                                          
                                                                                                          

2、接收方

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

#define PORT 6666                                                                                           
#define GRP_IP "224.1.2.3"
#define LOL_IP "192.168.124.51"

#define ERR_MSG(msg) do{\
    fprintf(stderr, "line:%d\n", __LINE__);\
    fprintf(stderr, "func:%s\n", __func__);\
    perror(msg);\
}while(0);

int main(int argc, const char *argv[])
{
    //创建套接字
    int sfd = socket(AF_INET, SOCK_DGRAM,0);
    if(-1 == sfd){
        ERR_MSG("socket");
        return -1;
    }
    printf("创建套接字成功\n");

    //允许端口被重复复用
    int reuse = 1;
    if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        ERR_MSG("setsockopt");
        return -1;
    }
    printf("允许端口快速重用\n");

    //加入多播组
    struct ip_mreqn mq;
    mq.imr_multiaddr.s_addr = inet_addr(GRP_IP);
    mq.imr_address.s_addr = inet_addr(LOL_IP);
    mq.imr_ifindex = 2; //网络设备索引
    if(-1 == setsockopt(sfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mq, sizeof(mq))){
        ERR_MSG("setsockopt");
        return -1;
    }
    printf("加入多播组[%s : %d] 成功\n", GRP_IP, PORT);


    struct sockaddr_in sender;   //存储对端的地址信息
    sender.sin_family = AF_INET;
    sender.sin_port = htons(PORT);
    sender.sin_addr.s_addr = inet_addr(GRP_IP);
    if(-1 == bind(sfd, (struct sockaddr*)&sender, sizeof(sender))){
        ERR_MSG("bind");
        return -1;
    }
    printf("绑定成功\n");

    int res;
    char buf[128] = "";
    struct sockaddr_in cin;
    socklen_t addr_len = sizeof(cin);
    while(1){
        //客户端收数据
        res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, &addr_len);
        if(-1 == res){
            ERR_MSG("recvfrom");
            return -1;
        }
        printf("[%s:%d] :%s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
        memset(buf, 0, sizeof(buf));
    }

    //关闭套接字
    close(sfd);
    return 0;
}
                                                                                                            
                                                                                                            

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值