在 Linux 中通过 Sockets 发送广播

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6777946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:11:04  来源:igfitidea点击:

Sending broadcast in Linux via Sockets

clinuxsocketsnetworkingbroadcast

提问by shaggy

SOLVEDplease close the question (but I do not really know how :/ bad day)

已解决,请关闭问题(但我真的不知道如何:/糟糕的一天)

Im trying to send broadcast in linux via sockets, it was always going out through both interfaces(ive got active eth0 and eth1, both in a different segments), but suddelny, it is going out just through the first one, eth0

我试图通过套接字在 linux 中发送广播,它总是通过两个接口传出(我有活动的 eth0 和 eth1,都在不同的段中),但是 suddelny,它只是通过第一个 eth0 传出

Here is the code:

这是代码:

void sendBroad(char *dstIP, char *localIP)
{
    int sock;                         /* Socket */
    struct sockaddr_in broadcastAddr; /* Broadcast address */
    int broadcastPermission;          /* Socket opt to set permission to broadcast */
    unsigned int localIPLen;       /* Length of string to broadcast */


    /* Create socket for sending/receiving datagrams */
    if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
        perror("socket() failed");

    /* Set socket to allow broadcast */
    broadcastPermission = 1;
    if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission, 
          sizeof(broadcastPermission)) < 0)
        perror("setsockopt() failed");

    /* Construct local address structure */
    memset(&broadcastAddr, 0, sizeof(broadcastAddr));   /* Zero out structure */
    broadcastAddr.sin_family = AF_INET;                 /* Internet address family */
    broadcastAddr.sin_addr.s_addr = inet_addr(dstIP);   /* Broadcast IP address */
    broadcastAddr.sin_port = htons(BroadcastPort);      /* Broadcast port */

    localIPLen = strlen(localIP);  /* Find length of localIP */
    int j;
    for (j=0; j<1; j++) //doesnt mean anything so far, not important
    {
         /* Broadcast localIP in datagram to clients */
         if (sendto(sock, localIP, localIPLen, 0, (struct sockaddr *) 
               &broadcastAddr, sizeof(broadcastAddr)) != localIPLen)
             perror("sendto() sent a different number of bytes than expected");


    }
}

Any help with this issue?

对这个问题有什么帮助吗?

Thanks in advance!

提前致谢!

采纳答案by shaggy

Just had to make fix the broadcast ports, it was mixed. The code itself is ok

只需要修复广播端口,它是混合的。代码本身没问题

回答by user207421

Your code looks OK at a quick glance. The problem may have been in the destination IP address.

快速浏览一下您的代码看起来不错。问题可能出在目标 IP 地址上。

NB you realize that a datagram arrives along with its source address anyway? You don't need to put the address into the payload as well. You could put something more specific in there such as an application identifier.

注意,您是否意识到数据报随其源地址一起到达?您也不需要将地址放入有效负载中。您可以在其中放置更具体的内容,例如应用程序标识符。