linux下的多播

本文介绍了在Linux下进行多播编程的步骤,包括关闭防火墙、创建UDP套接字,以及服务器端和客户端的代码实现。服务器端通过`sendto`函数向多播组发送消息,客户端使用`recvfrom`接收消息并加入/退出多播组。
摘要由CSDN通过智能技术生成


进行Linux下的多播编程时,需要关闭防火墙,关闭防火墙的方法如下:

1) 重启后生效
开启: chkconfig iptables on
关闭: chkconfig iptables off

2) 即时生效,重启后失效
开启: service iptables start
关闭: service iptables stop

 

服务器端代码:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>

int main()
{
 int sockfd;
 sockfd = socket(AF_INET, SOCK_DGRAM, 0);//UDP协议的套接字
 if (-1 == sockfd)
 {
  perror("socket");
  exit(1);
 }
 
 struct sockaddr_in address;
 memset(&address, 0, sizeof(address));
 address.sin_family = AF_INET;
 address.sin_addr.s_addr = inet_addr("224.0.0.99");//多播组地址
 address.sin_port = htons(6789);//多播组端口号
 
 while(1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值