请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
器件型号:MSP432E401Y 如何使广播 UDP 数据报正常工作?
UDP 数据报到定向 IP 地址的工作方式。
我从 udpecho TIRTOS 示例项目开始。
NDK 通过 udpecho.syscfg 进行控制。
CCS 12.5.0.0000067
C:\ti\simplelink_msp432e4_sdk_4_20_00_12
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);