ZHCU938C May 2018 – January 2021 CC3100 , CC3100MOD , CC3200 , CC3200MOD
在连续 mDNS 查询中,收到一个响应并不一定意味着不会有更多相关响应,并且查询操作将继续进行,直到不再需要响应为止。一次性查询是默认设置,因此需要进行额外设置才能使用连续查询。但是,由于 sl_NetAppDnsGetHostByService() 一次只返回一台发现的主机,请使用 sl_NetAppGetServiceList() 获取完整的服务列表。下面是一个返回列表格式为 SlNetAppGetFullServiceWithTextIpv4List_t
的示例:
#define LISTEN_SERVICE_NAME "_workstation._tcp.local"
#define SERVICE_GET_COUNT_MAX 3 // Specify the max number of services to discover
void mDNS_Query_Continuous()
{
_u32 pAddr;
_u32 usPort;
_u16 ulTextLen;
_i8 cText[200];
_i32 iretvalmDNS = -1;
_i32 i;
// The structure to be used for retrieving the list
SlNetAppGetFullServiceWithTextIpv4List_t serviceList[SERVICE_GET_COUNT_MAX];
// Set to perform Continuous mDNS query
iretvalmDNS = sl_NetAppSet(SL_NET_APP_MDNS_ID, NETAPP_SET_GET_MDNS_CONT_QUERY_OPT, 0, 0);
Report("sl_NetAppSet() return: %d\n\r", iretvalmDNS);
// Set an infinite loop until getting a response
iretvalmDNS = 1;
while(iretvalmDNS)
{
ulTextLen = 200;
iretvalmDNS = sl_NetAppDnsGetHostByService(LISTEN_SERVICE_NAME,
(unsigned char) strlen(LISTEN_SERVICE_NAME), SL_AF_INET,
(_u32 *)&pAddr, &usPort,&ulTextLen,&cText[0]);
}
if(iretvalmDNS == 0)
{
// Prints the first response, but we want more later on
cText[ulTextLen] = '\0';
Report("First Response: Addr: %d.%d.%d.%d, Port: %d, Text: %s, TextLength: %d\n\r",
SL_IPV4_BYTE(pAddr, 3), SL_IPV4_BYTE(pAddr, 2),
SL_IPV4_BYTE(pAddr, 1), SL_IPV4_BYTE(pAddr, 0),
usPort, cText, ulTextLen);
}
// Get a list of discovered services
iretvalmDNS = sl_NetAppGetServiceList(0,
SERVICE_GET_COUNT_MAX,
SL_NET_APP_FULL_SERVICE_WITH_TEXT_IPV4_TYPE,
(_i8*) &serviceList[0],
1479);
// 1479 is the maximum number of characters to return
// The function returns the total number of discovered services
// This number will never exceed the SERVICE_GET_COUNT_MAX parameter we passed into the
// function
Report("sl_NetAppSet() return: %d\n\r", iretvalmDNS);
if(iretvalmDNS > 0)
{
// Now just prints all the services
Report("Complete List:\n\r");
for(i=0; i<iretvalmDNS; i++)
{
Report("Host %d: %s, IP: %d.%d.%d.%d, Name: %s Port: %d, Text: %s\n\r",
i,
serviceList[i].service_host,
SL_IPV4_BYTE(serviceList[i].service_ipv4, 3),
SL_IPV4_BYTE(serviceList[i].service_ipv4, 2),
SL_IPV4_BYTE(serviceList[i].service_ipv4, 1),
SL_IPV4_BYTE(serviceList[i].service_ipv4, 0),
serviceList[i].service_name,
serviceList[i].service_port,
serviceList[i].service_text);
}
}
}