C ++如何加入多播mpeg流?

时间:2016-12-21 08:56:57

标签: c++ sockets udp mpeg2-ts multicastsocket

如何设置加入地址为udp://@224.1.50.15:1234的多播mpeg流组?我想管理的只是与流服务器建立连接,使服务器意识到我对这个组播组感兴趣,使服务器在一段时间内向我发送流数据包。

举个例子: 在使用VLC Player建立与组的连接之前,我没有收到来自此多播组的任何数据包。即使在我关闭VLC播放器之后,我仍然会在短时间内收到来自该组的数据包。我想要做的是在没有VLC播放器或任何其他第三方应用程序的帮助下接收数据包。

如何为上述目的设法与此群组建立联系。如果有可能吗?

P.S:我安装了4个网络接口。

谢谢!

正如Zaboj Campula在本页的回答中所提出的建议;我改变了如下代码。这给了我10093的错误。

struct ip_mreq {
    struct in_addr imr_multiaddr;   /* IP multicast address of group */
    struct in_addr imr_interface;   /* local IP address of interface */
};
SOCKET        s;
SOCKADDR_IN    localif;
struct ip_mreq mreq;
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
localif.sin_family = AF_INET;
localif.sin_port = htons(1234);
localif.sin_addr.s_addr = INADDR_ANY;
bind(s, (SOCKADDR *)&localif, sizeof(localif));
mreq.imr_interface.s_addr = INADDR_ANY; //Writing here my local ip didn't change the result.
mreq.imr_multiaddr.s_addr = inet_addr("224.1.50.15");
int rc = NO_ERROR;
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
// Join the group
rc = setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq));
if (rc == SOCKET_ERROR)
{
    printf("JoinMulticastGroup: setsockopt failed with error code %d\n", WSAGetLastError());
}
else
{
    printf("Joined group: ");
    //PrintAddress(group->ai_addr, group->ai_addrlen);
    printf("\n");
}

2 个答案:

答案 0 :(得分:1)

UDP套接字不支持连接。无法在UDP套接字上调用listenaccept。当UDP套接字绑定到本地端口时,您可以直接从UDP套接字读取。

套接字选项IP_ADD_MEMBERSHIP将套接字加入多播组。

 struct ip_mreq m;
 m.imr_interface.s_addr = inet_addr(loca_ip_address);
 m.imr_multiaddr.s_addr = inet_addr("224.1.50.15");
 setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&m, sizeof(m));

您可以在http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedmulticast9a.html

找到完整的示例

答案 1 :(得分:0)

好的,我设法在本书的帮助下解决了我的问题 Multicast Sockets Practical Guide for Programmers which is written by David Makofske & Kevin Almeroth

感谢作者和Zaboj Campula的努力。

我希望这篇文章和答案将来会帮助很多人。 工作代码写在下面! 使用Visual Studio 2017 RC(Console Empty C项目)编译

int sock;                     /* socket descriptor */
int flag_on = 1;              /* socket option flag */
struct sockaddr_in mc_addr;   /* socket address structure */
char recv_str[MAX_LEN + 1];     /* buffer to receive string */
int recv_len;                 /* length of string received */
struct ip_mreq mc_req;        /* multicast request structure */
char* mc_addr_str;            /* multicast IP address */
unsigned short mc_port;       /* multicast port */
struct sockaddr_in from_addr; /* packet source */
unsigned int from_len;        /* source addr length */
WSADATA wsaData;              /* Windows socket DLL structure */

mc_addr_str = "224.1.50.15";      /* arg 1: multicast ip address */
mc_port = atoi("1234");    /* arg 2: multicast port number */

                            /* validate the port range */
if ((mc_port < MIN_PORT) || (mc_port > MAX_PORT)) {
    fprintf(stderr, "Invalid port number argument %d.\n",
        mc_port);
    fprintf(stderr, "Valid range is between %d and %d.\n",
        MIN_PORT, MAX_PORT);
    exit(1);
}

/* Load Winsock 2.0 DLL */
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
    fprintf(stderr, "WSAStartup() failed");
    exit(1);
}

/* create socket to join multicast group on */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    perror("socket() failed");
    exit(1);
}

/* set reuse port to on to allow multiple binds per host */
if ((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&flag_on,
    sizeof(flag_on))) < 0) {
    perror("setsockopt() failed");
    exit(1);
}

/* construct a multicast address structure */
memset(&mc_addr, 0, sizeof(mc_addr));
mc_addr.sin_family = AF_INET;
mc_addr.sin_addr.s_addr = htonl(INADDR_ANY);
mc_addr.sin_port = htons(mc_port);

/* bind to multicast address to socket */
if ((bind(sock, (struct sockaddr *) &mc_addr,
    sizeof(mc_addr))) < 0) {
    perror("bind() failed");
    exit(1);
}

/* construct an IGMP join request structure */
mc_req.imr_multiaddr.s_addr = inet_addr(mc_addr_str);
mc_req.imr_interface.s_addr = htonl(INADDR_ANY);

/* send an ADD MEMBERSHIP message via setsockopt */
if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
    (char*)&mc_req, sizeof(mc_req))) < 0) {
    perror("setsockopt() failed");
    exit(1);
}

for (;;) {          /* loop forever */

                    /* clear the receive buffers & structs */
    memset(recv_str, 0, sizeof(recv_str));
    from_len = sizeof(from_addr);
    memset(&from_addr, 0, from_len);

    /* block waiting to receive a packet */
    if ((recv_len = recvfrom(sock, recv_str, MAX_LEN, 0,
        (struct sockaddr*)&from_addr, &from_len)) < 0) {
        perror("recvfrom() failed");
        exit(1);
    }

    /* output received string */
    printf("Received %d bytes from %s: ", recv_len,
        inet_ntoa(from_addr.sin_addr));
    printf("%s", recv_str);
}

/* send a DROP MEMBERSHIP message via setsockopt */
if ((setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
    (void*)&mc_req, sizeof(mc_req))) < 0) {
    perror("setsockopt() failed");
    exit(1);
}

closesocket(sock);
WSACleanup();  /* Cleanup Winsock */