C-macOS中的多播

C-macOS中的多播,c,macos,sockets,multicast,multicastsocket,C,Macos,Sockets,Multicast,Multicastsocket,同一主机(macOS)上的两个应用程序之间的多播。该示例基于启动两个应用程序Appli_A,它们加入同一个多播组并侦听由应用程序Appli_B发送的传入数据报数据包,但两个应用程序Appli_A中只有一个接收数据包,而不是两个都接收数据包。 由于实现依赖于平台,而且我正在使用macOS,所以我不得不更改一些命令,这就是为什么我要使用macOS 但还是不行。下面是一个小例子: 应用A #包括 #包括 #包括 #包括,但在MacOS下仍然无法运行。我试图编译这些程序,以便在我的机器上试用,但由于缺少

同一主机(macOS)上的两个应用程序之间的多播。该示例基于启动两个应用程序Appli_A,它们加入同一个多播组并侦听由应用程序Appli_B发送的传入数据报数据包,但两个应用程序Appli_A中只有一个接收数据包,而不是两个都接收数据包。 由于实现依赖于平台,而且我正在使用macOS,所以我不得不更改一些命令,这就是为什么我要使用macOS 但还是不行。下面是一个小例子:

应用A

#包括
#包括
#包括

#包括,但在MacOS下仍然无法运行。

我试图编译这些程序,以便在我的机器上试用,但由于缺少一些代码,两个程序都无法编译。因为我不能编译这些程序,所以我不能运行它们,因为我不能运行它们,所以我不能诊断它们。@Jeremy Friesner我根据Lyso_REUSEPORT更新了代码。只有在每个人都设置它的情况下,REUSEPORT才能工作。但是,如果第二个应用程序只发送,它不需要加入组,也不需要使用相同的端口。那么“模拟多播”是什么意思?这里没有模拟。
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

struct sockaddr_in localSock;
struct ip_mreq group;
int sd;
int datalen;
char databuf[1024];

void sigint_routine_handler(int param){
    if(close(sd) == 0)
        printf("hSocket closed\n");
    else
        printf("Error closing hSocket\n");

}


int main(int argc, char *argv[])
{
if(signal(SIGINT, sigint_routine_handler) == SIG_ERR)
    printf("ERROR: Installing suspend handler SIGINT\n");

sd = socket(AF_INET, SOCK_DGRAM, 0);
if(sd < 0)
{
    perror("Opening datagram socket error");
    exit(1);
}

/* Enable SO_REUSEPORT to allow multiple instances of this */
/* application to receive copies of the multicast datagrams. */

int reuse = 1;
if(setsockopt(sd, SOL_SOCKET, SO_REUSEPORT, (char *)&reuse, sizeof(reuse)) < 0)
{
    close(sd);
    exit(1);
}

/* Since I'm using htonl(INADDR_ANY) I'm Binding the socket to all available interfaces */
memset((char *) &localSock, 0, sizeof(localSock));
localSock.sin_family = AF_INET;
localSock.sin_port = htons(54011);
localSock.sin_addr.s_addr = htonl(INADDR_ANY);

if(bind(sd, (struct sockaddr*)&localSock, sizeof(localSock)))
{
    close(sd);
    exit(1);
}

/* Join the multicast group 235.73.158.23 */
group.imr_multiaddr.s_addr = inet_addr("235.73.158.23");
group.imr_interface.s_addr = htonl(INADDR_ANY);
if(setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0)
{
    close(sd);
    exit(1);
}

/* Read from the socket. */
datalen = sizeof(databuf);
if(read(sd, databuf, datalen) < 0)
{
    perror("Reading datagram message error");
    close(sd);
    exit(1);
}
else
{
    printf("Reading datagram message...OK.\n");
    printf("The message from multicast server is: \"%s\"\n", databuf);
}
if(close(sd) < 0)
        printf("Error close socket descriptio\n");

    return 0;
}
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

struct in_addr localInterface;
struct sockaddr_in groupSock;
int sd;
char databuf[50] = "Multicast test message$";
int datalen = sizeof(databuf);

int main(int argc, char *argv[])
{
/* Create a datagram socket on which to send. */
sd = socket(AF_INET, SOCK_DGRAM, 0);
if(sd < 0)
{
    perror("Opening datagram socket error");
    exit(1);
}
else
    printf("Opening the datagram socket...OK.\n");

memset((char *) &groupSock, 0, sizeof(groupSock));
groupSock.sin_family = AF_INET;
/* README Normally here should I specifiy the Multicast-adress of the group for the outgoing
   Datagrampacket, but it won't work if I specify this Multicast-adress "235.73.158.23"*/
groupSock.sin_addr.s_addr = htonl(INADDR_ANY);
groupSock.sin_port = htons(54011);

/* Set local interface for outbound multicast datagrams. */
/* The IP address specified must be associated with a local, */
/* multicast capable interface -> I have checked the interface, it is Multicast capable */
localInterface.s_addr = inet_addr("192.168.1.5");
if(setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, (char *)&localInterface, sizeof(localInterface)) < 0)
{
    perror("Setting local interface error");
    exit(1);
}
else
    printf("Setting the local interface...OK\n");
/* Send a message to the multicast group specified by the*/
/* groupSock sockaddr structure. */
int datalen = 50;
if(sendto(sd, databuf, datalen, 0, (struct sockaddr*)&groupSock, sizeof(groupSock)) < 0){
    perror("Sending datagram message error");
}
else
    printf("Sending datagram message...OK\n");
return 0;
}