esp8266使用函数espconn_gethostbyname()解析失败的问题

ip_addr_t *esp_server_ip;
LOCAL void ICACHE_FLASH_ATTR
user_esp_platform_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
{
struct espconn *pespconn = (struct espconn *)arg;
if (ipaddr != NULL)
os_printf(user_esp_platform_dns_found %d.%d.%d.%d/n,
*((uint8 *)&ipaddr->addr), *((uint8 *)&ipaddr->addr + 1),
*((uint8 *)&ipaddr->addr + 2), *((uint8 *)&ipaddr->addr + 3));
}
void dns_test(void) {
espconn_gethostbyname(pespconn,“iot.espressif.cn”, esp_server_ip,
user_esp_platform_dns_found);
}
如上DSN解析一般会成功但是会有出错的情况,我今天被这个困扰了很久。
 
ip_addr_t *esp_server_ip;
espconn_gethostbyname(pespconn,“iot.espressif.cn”, esp_server_
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
服务端代码: ```c #include "ets_sys.h" #include "osapi.h" #include "gpio.h" #include "os_type.h" #include "user_interface.h" #include "espconn.h" #define SERVER_PORT 8080 LOCAL struct espconn serverConn; LOCAL esp_tcp serverTcp; LOCAL struct espconn clientConn; LOCAL esp_tcp clientTcp; LOCAL char recvBuffer[128]; LOCAL char sendBuffer[128]; LOCAL void ICACHE_FLASH_ATTR serverRecvCb(void *arg, char *data, unsigned short len) { os_memcpy(sendBuffer, data, len); espconn_send(&clientConn, sendBuffer, len); } LOCAL void ICACHE_FLASH_ATTR clientRecvCb(void *arg, char *data, unsigned short len) { os_memcpy(sendBuffer, data, len); espconn_send(&serverConn, sendBuffer, len); } LOCAL void ICACHE_FLASH_ATTR clientConnectCb(void *arg) { os_printf("Client connected\r\n"); espconn_regist_recvcb(&clientConn, clientRecvCb); } LOCAL void ICACHE_FLASH_ATTR serverConnectCb(void *arg) { os_printf("Server connected\r\n"); espconn_regist_recvcb(&serverConn, serverRecvCb); } LOCAL void ICACHE_FLASH_ATTR serverDisconnectCb(void *arg) { os_printf("Server disconnected\r\n"); } LOCAL void ICACHE_FLASH_ATTR clientDisconnectCb(void *arg) { os_printf("Client disconnected\r\n"); } void ICACHE_FLASH_ATTR user_init() { os_printf("TCP Passthrough Server Demo\r\n"); // Initialize the server connection os_memset(&serverTcp, 0, sizeof(esp_tcp)); serverTcp.local_port = SERVER_PORT; os_memset(&serverConn, 0, sizeof(struct espconn)); serverConn.type = ESPCONN_TCP; serverConn.state = ESPCONN_NONE; serverConn.proto.tcp = &serverTcp; espconn_regist_connectcb(&serverConn, serverConnectCb); espconn_regist_disconcb(&serverConn, serverDisconnectCb); espconn_accept(&serverConn); // Initialize the client connection os_memset(&clientTcp, 0, sizeof(esp_tcp)); clientTcp.local_port = espconn_port(); os_memset(&clientConn, 0, sizeof(struct espconn)); clientConn.type = ESPCONN_TCP; clientConn.state = ESPCONN_NONE; clientConn.proto.tcp = &clientTcp; espconn_regist_connectcb(&clientConn, clientConnectCb); espconn_regist_disconcb(&clientConn, clientDisconnectCb); espconn_connect(&clientConn, (ip_addr_t *)&serverConn.proto.tcp->remote_ip, SERVER_PORT); } ``` 客户端代码: ```c #include "ets_sys.h" #include "osapi.h" #include "gpio.h" #include "os_type.h" #include "user_interface.h" #include "espconn.h" #define SERVER_IP "192.168.1.100" #define SERVER_PORT 8080 LOCAL struct espconn clientConn; LOCAL esp_tcp clientTcp; LOCAL struct espconn serverConn; LOCAL esp_tcp serverTcp; LOCAL char recvBuffer[128]; LOCAL char sendBuffer[128]; LOCAL void ICACHE_FLASH_ATTR clientRecvCb(void *arg, char *data, unsigned short len) { os_memcpy(sendBuffer, data, len); espconn_send(&serverConn, sendBuffer, len); } LOCAL void ICACHE_FLASH_ATTR serverRecvCb(void *arg, char *data, unsigned short len) { os_memcpy(sendBuffer, data, len); espconn_send(&clientConn, sendBuffer, len); } LOCAL void ICACHE_FLASH_ATTR serverConnectCb(void *arg) { os_printf("Server connected\r\n"); espconn_regist_recvcb(&serverConn, serverRecvCb); } LOCAL void ICACHE_FLASH_ATTR serverDisconnectCb(void *arg) { os_printf("Server disconnected\r\n"); } LOCAL void ICACHE_FLASH_ATTR clientConnectCb(void *arg) { os_printf("Client connected\r\n"); espconn_regist_recvcb(&clientConn, clientRecvCb); } LOCAL void ICACHE_FLASH_ATTR clientDisconnectCb(void *arg) { os_printf("Client disconnected\r\n"); } void ICACHE_FLASH_ATTR user_init() { os_printf("TCP Passthrough Client Demo\r\n"); // Initialize the client connection os_memset(&clientTcp, 0, sizeof(esp_tcp)); clientTcp.remote_port = SERVER_PORT; espconn_ip_addr_t serverIp; espconn_gethostbyname(&clientConn, SERVER_IP, &serverIp, espconn_dns_cb); os_memcpy(clientTcp.remote_ip, &serverIp.addr, 4); os_memset(&clientConn, 0, sizeof(struct espconn)); clientConn.type = ESPCONN_TCP; clientConn.state = ESPCONN_NONE; clientConn.proto.tcp = &clientTcp; espconn_regist_connectcb(&clientConn, clientConnectCb); espconn_regist_disconcb(&clientConn, clientDisconnectCb); espconn_connect(&clientConn); // Initialize the server connection os_memset(&serverTcp, 0, sizeof(esp_tcp)); serverTcp.local_port = espconn_port(); os_memset(&serverConn, 0, sizeof(struct espconn)); serverConn.type = ESPCONN_TCP; serverConn.state = ESPCONN_NONE; serverConn.proto.tcp = &serverTcp; espconn_regist_connectcb(&serverConn, serverConnectCb); espconn_regist_disconcb(&serverConn, serverDisconnectCb); espconn_accept(&serverConn); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值