多播的测试代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
int main(int argc, char*argv[])
{
int sockfd; // 套接字文件描述符
struct sockaddr_in local_addr; // 本地地址
int err = -1;
char group[16] = "224.0.0.88"; // 多播组 IP
sockfd = socket(AF_INET, SOCK_DGRAM, 0); //建立套接字
if (sockfd == -1)
{
perror("socket()");
return -1;
}
// 初始化地址
memset(&local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
local_addr.sin_port = htons(8080);
// 绑定socket
err = bind(sockfd,(struct sockaddr*)&local_