This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
如何使广播 UDP 数据报正常工作?
UDP 数据报到定向 IP 地址的工作方式。
我从 udpecho TIRTOS 示例项目开始。
NDK 通过 udpecho.syscfg 进行控制。
CCS 12.5.0.0000067
C:\ti\simplelink_msp432e4_sdk_4_20_00_12
您好!
我目前正在旅行。 我将在下周回复您的帖子。
根据我的理解(即、这似乎有效)、发送多播只需发送到正确的多播地址。
例如
const char * MULTICAST = "224.0.1.129"; struct sockaddr_in clientAddr; socklen_t addrlen = sizeof(clientAddr); clientAddr.sin_family = AF_INET; clientAddr.sin_port = htons(EVENT_PORT); clientAddr.sin_addr.s_addr = inet_addr(MULTICAST); // The required IP address int bytesSent = sendto(LocalFD, pBuffer, bytesToSend, 0, (struct sockaddr *)&clientAddr, addrlen); if (bytesSent < 0) { Display_printf(display, 0, 0, "Error: sendto failed."); }
要从多播接收,必须先注册多播:
/// A temporary struct used for setting multicast address. typedef struct my_ip_mreq { struct in_addr imr_multiaddr; /**< IP Address of the group */ struct in_addr imr_interface; /**< IP Address of the interface */ }my_ip_mreq; struct my_ip_mreq mreq; mreq.imr_multiaddr.s_addr = inet_addr(MULTICAST); mreq.imr_interface.s_addr = htonl(INADDR_ANY); int res = setsockopt(LocalFD, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); Display_printf(display, 0, 0, "Join 1st multicast on PTP port returned %d", res); /* Set the SO_REUSEPORT option. */ int optval = 1; res = setsockopt(LocalFD, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval)); Display_printf(display, 0, 0, "Set SO_REUSEPORT on PTP port returned %d", res);
谢谢。 这只是多播接收的票据。
这很好、因为我意识到您实际上问的是广播、而不是多播!
对于 Broadcast、我相信您只需将所有本地地址位都设置为1的地址。 例如、如果您的网络为192.168.0.n、则您将广播到192.168.0.255 -上面有一篇很好的 Wikipedia 文章
对于广播、我需要:
int ALLOW_BROADCAST = 1;
STATUS = setsockopt (server、SOL_SOCKET、SO_broadcast、(void*)&allow_broadcast、sizeof (allow_broadcast));
否则,sendto ()失败。