首页 > 系统运维 > 浅谈广播、组播

浅谈广播、组播

标 签:

一 、广播

广播地址为 最大的主机地址,整个网段内都能收到广播数据
例如192.168.0.0网段,广播地址为192.168.0.255

广播设置
发送端设置socket属性并设置发送地址为广播地址
    int on = 1; 
    setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
接受端,正常接收广播数据

send.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>

#define BUF_SIZE 100

int main()
{
    //socket
    int iClient = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == iClient)
    {
        return -1;
    }
    printf("create socket ok\r\n");
    //set broadcast
    int on = 1;
    int iRet = setsockopt(iClient, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));  
    if (-1 == iRet)
    {
        return -1;
    }
    struct sockaddr_in stServer;
    memset(&stServer, 0, sizeof(struct sockaddr_in));
    stServer.sin_family = AF_INET;
    stServer.sin_port = htons(8888);
    stServer.sin_addr.s_addr = inet_addr("192.168.0.255");
    //recvfrom sendto
    char buf[BUF_SIZE] = {0};
    struct sockaddr_in stClient;
    socklen_t tLen = sizeof(struct sockaddr_in);
    iRet = -1;
    while(1)
    {
        memset(buf, 0, BUF_SIZE);
        memset(&stClient, 0, sizeof(struct sockaddr_in));
        gets(buf);
        iRet = sendto(iClient, buf, BUF_SIZE, 0, (struct sockaddr *)&stServer, tLen);
        //if (iRet > 0)
        {
            //memset(buf, 0, BUF_SIZE);
            //recvfrom(iClient, buf, BUF_SIZE, 0, (struct sockaddr *)&stClient, &tLen);
            //printf("client : %s\r\n", buf);
        }
    }
    return 0;
}

read.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>

#define BUF_SIZE 100

int main()
{
    //socket
    int iServer = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == iServer)
    {
        return -1;
    }
    printf("create socket ok\r\n");
    //bind
    struct sockaddr_in stServer;
    memset(&stServer, 0, sizeof(struct sockaddr_in));
    stServer.sin_family = AF_INET;
    stServer.sin_port = htons(8888);
    stServer.sin_addr.s_addr = INADDR_ANY;
    int iRet = bind(iServer, (struct sockaddr *)&stServer, sizeof(struct sockaddr_in));
    if (-1 == iRet)
    {
        return -1;
    }
    printf("bind ok\r\n");
    //recvfrom sendto
    char buf[BUF_SIZE] = {0};
    struct sockaddr_in stClient;
    socklen_t tLen = sizeof(struct sockaddr_in);
    while(1)
    {
        memset(buf, 0, BUF_SIZE);
        memset(&stClient, 0, sizeof(struct sockaddr_in));
        iRet = recvfrom(iServer, buf, BUF_SIZE, 0, (struct sockaddr *)&stClient, &tLen);
        if (iRet > 0)
        {
            printf("server : %s\r\n", buf);
            //sendto(iServer, buf, BUF_SIZE, 0, (struct sockaddr *)&stClient, tLen);
        }
    }
    return 0;
}

二 、组播

组播地址 D类 最高位为前4位固定为1110,224.0.0.1 – 239.255.255.255
例如 224.10.10.1
只有加入到组中才能接受到数据
设置方法
struct ip_mreq
{
    struct  in_addr  imr_multiaddr;
    struct  in_addr  imr_interface;
};

int on = 1;
setsockopt(iSocket, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
设置地址和端口重用

struct  ip_mreq  mreq;
bzero(&mreq, sizeof(mreq));
mreq.imr_multiaddr.s_addr = inet_addr(“224.10.10.1”);  组播地址
mreq.imr_interface.s_addr = htonl(INADDR_ANY);         接收组中任意的IP地址
setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,sizeof(mreq));
注意设置的是IP层,对应的协议是IGMP协议,组管理协议

send.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>

#define BUF_SIZE 100

int main()
{
    //socket
    int iClient = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == iClient)
    {
        return -1;
    }
    printf("create socket ok\r\n");

    struct sockaddr_in stServer;
    memset(&stServer, 0, sizeof(struct sockaddr_in));
    stServer.sin_family = AF_INET;
    stServer.sin_port = htons(8888);
    stServer.sin_addr.s_addr = inet_addr("224.10.10.1");
    //recvfrom sendto
    char buf[BUF_SIZE] = {0};
    struct sockaddr_in stClient;
    socklen_t tLen = sizeof(struct sockaddr_in);
    int iRet = -1;
    while(1)
    {
        memset(buf, 0, BUF_SIZE);
        memset(&stClient, 0, sizeof(struct sockaddr_in));
        gets(buf);
        iRet = sendto(iClient, buf, BUF_SIZE, 0, (struct sockaddr *)&stServer, tLen);
        /*if (iRet > 0)
        {
            memset(buf, 0, BUF_SIZE);
            recvfrom(iClient, buf, BUF_SIZE, 0, (struct sockaddr *)&stClient, &tLen);
            printf("client : %s\r\n", buf);
        }*/
    }
    return 0;
}

read.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>

#define BUF_SIZE 100

int main()
{
    //socket
    int iServer = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == iServer)
    {
        return -1;
    }
    printf("create socket ok\r\n");
    //membership
    struct  ip_mreq  mreq;
    bzero(&mreq, sizeof(mreq));
    mreq.imr_multiaddr.s_addr = inet_addr("224.10.10.1");
    mreq.imr_interface.s_addr = htonl(INADDR_ANY);
    int iRet = setsockopt(iServer, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
    if (-1 == iRet)
    {
        return -1;
    }
    //bind
    struct sockaddr_in stServer;
    memset(&stServer, 0, sizeof(struct sockaddr_in));
    stServer.sin_family = AF_INET;
    stServer.sin_port = htons(8888);
    stServer.sin_addr.s_addr = INADDR_ANY;
    iRet = bind(iServer, (struct sockaddr *)&stServer, sizeof(struct sockaddr_in));
    if (-1 == iRet)
    {
        return -1;
    }
    printf("bind ok\r\n");
    //recvfrom sendto
    char buf[BUF_SIZE] = {0};
    struct sockaddr_in stClient;
    socklen_t tLen = sizeof(struct sockaddr_in);
    while(1)
    {
        memset(buf, 0, BUF_SIZE);
        memset(&stClient, 0, sizeof(struct sockaddr_in));
        iRet = recvfrom(iServer, buf, BUF_SIZE, 0, (struct sockaddr *)&stClient, &tLen);
        if (iRet > 0)
        {
            printf("server : %s\r\n", buf);
            //sendto(iServer, buf, BUF_SIZE, 0, (struct sockaddr *)&stClient, tLen);
        }
    }
    return 0;
}
作者:hang_1994 发表于2017/1/9 9:59:31 原文链接
阅读:76 评论:0 查看评论
分类: 系统运维 标签: