0
bbebmsa

参考 qq_link的例程,准备在自己的工程中加入 get host by name 的功能,但是只要在工程中加入 h = lwip_gethostbyname(hostname); 函数,IAR go的时候就提示错误,程序也工作不正常了。提示如下:

Thu Apr 13, 2017 23:35:08: Macro '__load_dram_param' already defined in W:\sdk-ameba1-v3.4b3_without_NDA 20170411\project\realtek_ameba1_va0_example\EWARM-RELEASE7.8\..\..\..\component\soc\realtek\
8195a\misc\iar_utility\common\dram\EM6A6165TS_7G.mac; definition found in W:\sdk-ameba1-v3.4b3_without_NDA 20170411\project\realtek_ameba1_va0_example\EWARM-RELEASE7.8\..\..\..\component\soc\realtek\
8195a\misc\iar_utility\common\dram\EM6A6165TS_7G.mac ignored
Thu Apr 13, 2017 23:35:08: Loaded macro file: W:\sdk-ameba1-v3.4b3_without_NDA 20170411\project\realtek_ameba1_va0_example\EWARM-RELEASE7.8\..\..\..\component\soc\realtek\8195a\misc\iar_utility\common\
dram\EM6A6165TS_7G.mac
Thu Apr 13, 2017 23:35:09: There were 4 warnings during the initialization of the debugging session.

 

==============================================

在main()函数中 调用 GetHostN();

 
#include <stdio.h>
#include <string.h>
#include "FreeRTOS.h"
#include "task.h"
#include "udp.h"
#include <sockets.h>
#include <lwip_netconf.h>
#include "lwip/netdb.h"

#include "wifi_constants.h"
#include "NtpTime.h"

const char host_url[] = "http://img1.gtimg.com/13/1300/130025/13002509_980x1200_0.jpg";

int cli_sockfd;

unsigned long ulDestinationIP;

// url: scheme://[userinfo@]host/path[?query][#fragment]
void get_host_from_url(const uint8_t *url, uint8_t *host, uint32_t host_size)
{
uint32_t i, url_len, s, t;
url_len = strlen(url);

s = 0;
for (i = 0; i < url_len; ++i)
{
if (url[i] == '/')
{
s = i + 2;
break;
}
}

t = url_len;    // 整个字符串长度
for (i = s; i < url_len; ++i)
{
if (url[i] == '/')
{
t = i;
break;
}
}

// host: url[s,t)
if (s && t && (t-s) < host_size)
{
memcpy(host, url + s, t - s);
host[t-s] = '\0';
}
}

int GetSHostN(void)
{
struct sockaddr_in  cli_addr;
socklen_t addrlen;
struct hostent *Host = NULL;   // 保存 域名主机相关的参数

int lRetVal = 0;

char cDataBuf[48];
memset(cDataBuf, 0, sizeof(cDataBuf));
cDataBuf[0] = '\x1b';

uint8_t hostname[255] = {0};    // 保存域名的缓冲区

char *ip = NULL;    // 保存 ip的指针
struct hostent *h = NULL;   // 保存 域名主机相关的参数
get_host_from_url(host_url, hostname, sizeof(hostname)); // 从url中 获取 域名 字符串

h = lwip_gethostbyname(hostname);   // 通过主机名,获取主机相关信息
if (!h)
{   // NG
lRetVal = -1;
goto end;
}

ip = inet_ntoa(*((struct in_addr *)h->h_addr)); // 返回 存放ip的指针
if (!ip)
{   // NG
lRetVal = -1;
goto end;
}

printf("\n\rip: %s\n\r",ip);

if (lRetVal != sizeof(cDataBuf))
{
lRetVal = -1;
// could not send SNTP request
}

end:
return lRetVal;
}

void GetHostNTask(void *pvParameters)
{
int lRetVal = -1;

printf("\n\rGET_TIME: Test Begin\n\r");

while(wifi_is_ready_to_transceive(RTW_STA_INTERFACE) != RTW_SUCCESS)
{    //
vTaskDelay(50 / portTICK_RATE_MS);
}
printf("\n\rwifi connect successfully!");

cli_sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(cli_sockfd < 0)
{
return;
}

lRetVal = GetSHostN();
if(lRetVal < 0)
{
printf("\n\rfailed\n\r");
goto end;
}

end:
close(cli_sockfd);
printf("\n\rGetHostNTask end");

printf("\n\rGet Time end,Task delete!");
vTaskDelete(NULL);
}

int GetHostN(void)
{
if(xTaskCreate(GetHostNTask, ((const char*)"GetHostN"), 1024 * 4, NULL, tskIDLE_PRIORITY + 3, NULL) != pdPASS)
{
printf("\n\rxTaskCreate failed !");
return (-1);
}

return 0;
}

bbebmsa 回答了