linux组播bind本地ip,Linux网络的多播IP技术

/*

* listen.c - An IP multicast client */

#include

#include

#include

#include

#include

#include

#include

char * host_name = "224.0.0.1";

/* 多播IP地址 */

int port = 6789;

int main(void)

{

struct ip_mreq command;

int loop = 1;

/* 多播循环 */

int iter = 0;

int sin_len;

char message[256];

int socket_descriptor;

struct sockaddr_in sin;

struct hostent *server_host_name;

if((server_host_name = gethostbyname(host_name)) == 0)

{

perror("gethostbyname");

exit(EXIT_FAILURE);

}

/*bzero(&sin, sizeof(sin));*/

memset(&sin, 0, sizeof(sin));

sin.sin_family = AF_INET;

sin.sin_addr.s_addr = htonl(INADDR_ANY);

sin.sin_port = htons(port);

if((socket_descriptor = socket(PF_INET, SOCK_DGRAM, 0)) == -1)

{ perror("socket");

exit(EXIT_FAILURE);

}

/* 调用bind之前,设置套接口选项启用多播IP支持*/

loop = 1;

if(setsockopt(socket_descriptor,

SOL_SOCKET, SO_REUSEADDR,

&loop, sizeof(loop)) < 0)

{

perror("setsockopt:SO_REUSEADDR");

exit(EXIT_FAILURE);

}

if(bind(socket_descriptor,

(struct sockaddr *)&sin, sizeof(sin)) < 0)

{

perror("bind");

exit(EXIT_FAILURE);

}

/* 在同一个主机上进行广播设置套接口,

作用是方便单个开发系统上测试多播IP广播 */

loop = 1;

if(setsockopt(socket_descriptor,

IPPROTO_IP, IP_MULTICAST_LOOP,

&loop, sizeof(loop)) < 0)

{

perror("setsockopt:IP_MULTICAST_LOOP");

exit(EXIT_FAILURE);

}

/* 加入一个广播组。进一步告诉Linux内核,

特定的套接口即将接受广播数据*/

command.imr_multiaddr.s_addr = inet_addr("224.0.0.1");

command.imr_interface.s_addr = htonl(INADDR_ANY);

if(command.imr_multiaddr.s_addr == -1)

{

perror("224.0.0.1 not a legal multicast address");

exit(EXIT_FAILURE);

}

if (setsockopt(socket_descriptor, IPPROTO_IP, IP_ADD_MEMBERSHIP,

&command, sizeof(command)) < 0)

{

perror("setsockopt:IP_ADD_MEMBERSHIP");

}

while(iter++ < 8)

{

sin_len = sizeof(sin);

if(recvfrom(socket_descriptor, message, 256, 0,

(struct sockaddr *)&sin, &sin_len) == -1) {

perror("recvfrom");

}

printf("Response #%-2d from server: %s\n", iter, message);

sleep(2);    }

/* 接受8个广播后退出 */

if(setsockopt(socket_descriptor, IPPROTO_IP, IP_DROP_MEMBERSHIP,

&command, sizeof(command)) < 0) {

perror("setsockopt:IP_DROP_MEMBERSHIP");

}

close(socket_descriptor);

exit(EXIT_SUCCESS);}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值