在 WinSock 中通过 127.0.0.1 发送的多播可以用 INADDR_ANY 读取吗?

Posted

技术标签:

【中文标题】在 WinSock 中通过 127.0.0.1 发送的多播可以用 INADDR_ANY 读取吗?【英文标题】:Can Multicasts sent via 127.0.0.1 in WinSock be read with INADDR_ANY? 【发布时间】:2020-10-15 20:56:44 【问题描述】:

我有一个使用 WinSock 的简单多播写入器和读取器对(代码如下)。如果我没有指定多播的 IP 地址(使用 IP_MULTICAST_IF),我可以使用绑定到 INADDR_ANY 的套接字读取多播。如果我使用 IP_MULTICAST_IF 通过 127.0.0.1 发送多播,则使用 INADDR_ANY 读取不起作用。似乎将读取套接字绑定到 127.0.0.1 是查看通过 127.0.0.1 发送的多播的唯一方法。这是出乎意料的,因为我认为 INADDR_ANY 也监听了环回地址。有没有办法让多播阅读器获取所有多播,而不管使用哪个 IP 地址发送多播?

//CODE FOR MULTICAST WRITER
#include <sys/types.h>   /* for type definitions */
#include <winsock2.h>    /* for win socket API calls */
#include <ws2tcpip.h>    /* for win socket structs */
#include <stdio.h>       /* for printf() */
#include <stdlib.h>      /* for atoi() */
#include <string.h>      /* for strlen() */

const in_addr   MC_ADDR   =  225, 1, 1, 1 ;
const int       MC_PORT   = 1001;
const int       MAX_SEND  = 1024;
const in_addr   SEND_ADDR =  127, 0, 0, 1 ;

int main(int argc, char *argv[]) 

    int sock;                   /* socket descriptor */
    char send_str[MAX_SEND];    /* string to send */
    struct sockaddr_in mc_addr; /* socket address structure */
    int send_len;               /* length of string to send */
    WSADATA wsaData;            /* Windows socket DLL structure */

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

    /* create a socket for sending to the multicast address */
    if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) 
    
        perror("socket() failed");
        exit(1);
    

#if 1 //IF THIS IS DEFINED THE MULTICAST READER UST BE LISTENING TO 127.0.0.1
    // Configure Multicast
    in_addr myAddress =  127, 0, 0, 1 ;
    if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (char*)&myAddress, sizeof(myAddress)) < 0)
    
        perror("setsockopt(IP_MULTICAST_IF) failed");
        exit(1);
    
#endif

    char iLoopOn = 1;
    if ((setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, &iLoopOn, sizeof(iLoopOn))) < 0)
    
        perror("setsockopt(IP_MULTICAST_LOOP) 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 = MC_ADDR;
    mc_addr.sin_port = htons(MC_PORT);

    printf("Begin typing (return to send, ctrl-C to quit):\n");

    /* clear send buffer */
    memset(send_str, 0, sizeof(send_str));

    while (fgets(send_str, MAX_SEND, stdin)) 
    
        send_len = strlen(send_str);

        /* send string to multicast address */
        if ((sendto(sock, send_str, send_len, 0,
            (struct sockaddr *) &mc_addr,
            sizeof(mc_addr))) != send_len) 
        
            perror("sendto() sent incorrect number of bytes");
            exit(1);
        

        /* clear send buffer */
        memset(send_str, 0, sizeof(send_str));
    

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

    exit(0);

//CODE FOR MULTICAST READER
#include <sys/types.h>  /* for type definitions */
#include <winsock2.h>   /* for win socket API calls */
#include <ws2tcpip.h>   /* for win socket structs */
#include <stdio.h>      /* for printf() and fprintf() */
#include <stdlib.h>     /* for atoi() */
#include <string.h>     /* for strlen() */

const int       MC_PORT = 1001;
const int       MAX_RECV = 1024;
const ULONG     MC_ADDR = inet_addr("225.1.1.1");
#if 0 //THIS_IS_REQUIRED_WHEN_MULTICASTING_TO_127001
const ULONG     MY_ADDR = inet_addr("127.0.0.1");
#else
const ULONG     MY_ADDR = htonl(INADDR_ANY);
#endif

int main(int argc, char *argv[])

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

    /* 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, 0)) < 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("SO_REUSEADDR 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 = MY_ADDR;
    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 = MC_ADDR;
    mc_req.imr_interface.s_addr = MY_ADDR;

    /* send an ADD MEMBERSHIP message via setsockopt */
    if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mc_req, sizeof(mc_req))) < 0) 
    
        perror("B 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_RECV, 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, (const char*)&mc_req, sizeof(mc_req))) < 0) 
    
        perror("C setsockopt() failed");
        exit(1);
    

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

    exit(0);

【问题讨论】:

@rustyx 哦,对不起。我没有仔细检查代码,你是对的。很可能是你提到的原因。但无论如何,Windows 网络堆栈有点不同,TBH 我也从未在 *ix 系统上尝试过。 我正在使用 IP_MULTICAST_LOOP 来启用多播到发送方的环回。我不确定我是否需要在其他地方这样做。 为什么要将 IP_MULTICAST_IF 设置为 127.0.0.1?解决方案:不要。您是多播还是发送到本地主机? 原因是我有一个应用程序,它使用多播来传输可以被第三方应用程序获取的数据。有时该第三方应用程序驻留在同一台机器上。 【参考方案1】:

对于其他对此感兴趣的人,我没有找到一种方法让“ANY”也获得环回多播。我最终决定将成员资格添加到 ANY 适配器和环回地址。收货人代码如下。我确实遇到了一些关于 127.0.0.1 会员资格的问题,但忽略错误代码似乎让一切正常。

#include <sys/types.h>  /* for type definitions */
#include <winsock2.h>   /* for win socket API calls */
#include <ws2tcpip.h>   /* for win socket structs */
#include <stdio.h>      /* for printf() and fprintf() */
#include <stdlib.h>     /* for atoi() */
#include <string.h>     /* for strlen() */

const int       MC_PORT = 1001;
const int       MAX_RECV = 2000000;
const char     *MC_ADDR = "225.1.1.1";
const char     *LB_ADDR = "127.0.0.1";

void PrintSocketError(const char *Prefix)

    int iLastError = WSAGetLastError();
    if (iLastError != 0)
    
        fprintf(stderr, "%s: %d\n", Prefix, iLastError);
    

int main(int argc, char *argv[])

    int test;
    int recvsize_optval = MAX_RECV;
    int sock;                     /* socket descriptor */
    int flag_on = 1;              /* socket option flag */
    struct sockaddr_in mc_addr;   /* socket address structure */
    char *recv_str = new char[MAX_RECV+1];    /* buffer to receive string - make it 2MB so we can get packets from Cortex */
    int recv_len;                 /* length of string received */
    struct ip_mreq mc_req_any;    /* multicast request structure for "ANY" adapter */
    struct ip_mreq mc_req_lb;     /* multicast request structure for loopback adapter */
    struct sockaddr_in from_addr; /* packet source */
    int from_len;                 /* source addr length */
    WSADATA wsaData;              /* Windows socket DLL structure */

    /* Load Winsock 2.0 DLL */
    printf("Starting WinSock\n");
    if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) 
    
        fprintf(stderr, "WSAStartup() failed");
        exit(1);
    

    /* create socket to join multicast group on */
    printf("Creating Socket\n");
    if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    
        PrintSocketError("socket() failed");
        exit(1);
    

    /* set reuse port to on to allow multiple binds per host */
    printf("Reusing Address\n");
    if ((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&flag_on, sizeof(flag_on))) < 0)
    
        PrintSocketError("SO_REUSEADDR failed");
        exit(1);
    

    /* construct a socket address structure */
    printf("Binding\n");
    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) 
    
        PrintSocketError("bind() failed");
        goto END;
    

    /* ADD MEMBERSHIP to ANY adapter. This appears to not get messages
     * sent via 127.0.0.1, so we will add membership to 127.0.0.1 explictly below */
    printf("Adding membership to \"ANY\"\n");
    inet_pton(AF_INET, MC_ADDR, &mc_req_any.imr_multiaddr.s_addr);
    mc_req_any.imr_interface.s_addr = htonl(INADDR_ANY);
    if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mc_req_any, sizeof(mc_req_any))) < 0)
    
        PrintSocketError("IP_ADD_MEMBERSHIP(ANY) failed");
        goto END;
    

    /* send an ADD MEMBERSHIP message via setsockopt */
    /* buld multicast request structures for "ANY" adapater and loopback adapter */
    printf("Adding membership to \"loopback\"\n");
    inet_pton(AF_INET, MC_ADDR, &mc_req_lb.imr_multiaddr.s_addr);
    inet_pton(AF_INET, LB_ADDR, &mc_req_lb.imr_interface.s_addr);
    if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mc_req_lb, sizeof(mc_req_lb))) < 0)
    
        PrintSocketError("IP_ADD_MEMBERSHIP(LB) failed");
        //I have seen this generally mean that the membership is alreayd subscribed.
        //For some reason ANY subscribed to 127.0.0.1 can take a little while to release.
        //Print the error and ignore it
    

    // Large 1MB Buffer for receiving so we don't lose data.
    printf("Configuring RECV buffer size to %d\n", recvsize_optval);
    setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&recvsize_optval, sizeof(recvsize_optval));
    getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&recvsize_optval, NULL);
    if (recvsize_optval != 0x100000)
    
        printf("ReceiveBuffer size = %d\n", recvsize_optval);
    

    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_RECV, 0, (sockaddr*)&from_addr, &from_len)) < 0)
        
            char buf[100];
            sprintf_s(buf, "recvfrom() failed: %d %d", recv_len, WSAGetLastError());
            PrintSocketError(buf);
            printf("\n");
            goto END;
        

        /* output received string */
        char strFromAddr[128];
        inet_ntop(AF_INET, &from_addr.sin_addr, strFromAddr, sizeof(strFromAddr));
        printf("Received %d bytes from %s\n", recv_len, strFromAddr);
        //printf("%s", recv_str);

        if (recv_len == 1)
        
            printf("exiting\n");
            break;
        
    

END:
    delete[] recv_str;
    printf("cleaning up Winsock\n");
    closesocket(sock);//also drops memberships
    WSACleanup();  /* Cleanup Winsock */

    printf("done!\n");


【讨论】:

以上是关于在 WinSock 中通过 127.0.0.1 发送的多播可以用 INADDR_ANY 读取吗?的主要内容,如果未能解决你的问题,请参考以下文章

node中通过getpost方法得到客户端的信息

Gstreamer Gstreamer中通过UDP(RTP)远程播放MP3

Winsock 使用系统代理设置

请问:在VB6.0中使用winsock控件,作为服务器要设置本地IP地址吗?

如何在终端中请求新的 TOR 身份

windows通过Winscp传输文件给Vbox虚拟机