0
我正在嘗試使用lwip netconn API(使用esp-open-rtos)爲ESP8266編寫簡單的SNTP客戶端。問題是我無法從服務器接收答案。lwip netconn api - 無法接收來自SNTP服務器的答案
代碼(沒有錯誤檢查和調試messgaes):
#include <string.h>
#include <lwip/api.h>
#include <lwip/err.h>
struct sntp_message
{
u8_t li : 2;
u8_t vn : 3;
u8_t mode : 3;
u8_t stratum;
u8_t poll;
u8_t precision;
u32_t root_delay;
u32_t root_dispersion;
u32_t reference_identifier;
u32_t reference_timestamp[2];
u32_t originate_timestamp[2];
u32_t receive_timestamp[2];
u32_t transmit_timestamp[2];
} __attribute__((packed));
#define SNTP_MSG_LEN (sizeof(struct sntp_message))
int16_t sntp_sync(char* server)
{
err_t err;
int16_t result = ERR_OK;
ip_addr_t sntp_server_address;
struct netconn* connection = NULL;
struct netbuf* send_buffer = NULL;
struct netbuf* receive_buffer = NULL;
struct sntp_message* send_buffer_data = NULL;
err = netconn_gethostbyname(server, &sntp_server_address);
connection = netconn_new(NETCONN_UDP);
err = netconn_connect(connection, &sntp_server_address, 123);
send_buffer = netbuf_new();
send_buffer_data = netbuf_alloc(send_buffer, SNTP_MSG_LEN);
memset(send_buffer_data, 0, SNTP_MSG_LEN);
send_buffer_data->vn = 4;
send_buffer_data->mode = 3; // Mode client.
err = netconn_send(connection, send_buffer);
err = netconn_recv(connection, &receive_buffer); // Here netconn_recv block my thread, and no data received. If i set timeout, i have timeout error.
return result;
}
什麼都可以錯在我的代碼?在接收數據(通過netconn_bind)之前,我需要綁定我的連接嗎?或者我忘了其他的東西?
我試過這個實現,但是它失敗了 - lwip_recvfrom返回-1。 – Anton
它在我的項目中工作。檢查你的端口/驅動程序。而'lwipopts.h' – kyb
TCP通信正常工作我的項目。這會對UPD造成麻煩嗎? – Anton