进行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)