nRF9160 HTTP clien slow response

hi:

Use the https_client routine to send data after the socket is established, and then send the data after getting the response. The errno returns as 128, which can confirm that the cloud platform is not disconnected. In addition, the time from sending the request to getting the response is nearly 2 minutes, which is too long to receive. How can I improve the problem I'm encountering now, thank you

void main(void)
{
int err;
int fd;
char *p;
int bytes;
size_t off;
struct addrinfo *res;
struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
};
struct timeval timeout;//666
int data_len;
uint32_t connect_attempt = 0;

printk("http link onenet sample started\n\r");


printk("Waiting for network.. ");
do{
err = lte_lc_init_and_connect();
if (err) {
printk("Failed to connect to the LTE network, err %d\n", err);
k_sleep(K_SECONDS(CONFIG_HTTP_RECONNECT_DELAY_S));
}
}while(err);
printk("OK\n");


if (connect_attempt++ > 0) {
printk("Reconnecting in %d seconds...",CONFIG_HTTP_RECONNECT_DELAY_S);
k_sleep(K_SECONDS(CONFIG_HTTP_RECONNECT_DELAY_S));
}

err = getaddrinfo(HTTP_HOSTNAME, NULL, &hints, &res);
if (err) {
printk("getaddrinfo() failed, err %d\n", errno);
goto clean_up;
}

((struct sockaddr_in *)res->ai_addr)->sin_port = htons(HTTP_PORT);

if (IS_ENABLED(CONFIG_SAMPLE_TFM_MBEDTLS)) {
fd = socket(AF_INET, SOCK_STREAM | SOCK_NATIVE_TLS, IPPROTO_TLS_1_2);
} else {
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);// IPPROTO_TLS_1_2
}
if (fd == -1) {
printk("Failed to open socket!\n");
goto clean_up;
}

/*666*/
timeout.tv_sec = WEBCLIENT_DEFAULT_TIMEO;
timeout.tv_usec = 0;
/* set recv timeout option */
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void*) &timeout,
sizeof(timeout));
//setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (void*) &timeout,
// sizeof(timeout));
/*666*/

/* Setup TLS socket options */
//err = tls_setup(fd);
//if (err) {
// goto clean_up;
//}

printk("Connecting to %s\n", HTTP_HOSTNAME);
err = connect(fd, res->ai_addr, sizeof(struct sockaddr_in));
if (err) {
printk("connect() failed, err: %d\n", errno);
goto clean_up;
}

data_len = snprintf(send_buf,SEND_BUF_SIZE,"%s",HTTP_GET_DETECT);
off = 0;
do {
bytes = send(fd, &send_buf[off], data_len - off, 0);
if (bytes < 0) {
printk("send() failed, err %d\n", errno);
goto clean_up;
}
off += bytes;
} while (off < data_len);

printk("Sent %d bytes\n", off);

off = 0;
do {
bytes = recv(fd, &recv_buf[off], RECV_BUF_SIZE - off, 0);
if (bytes < 0) {
printk("recv() failed, err %d\n", errno);
goto clean_up;
}
off += bytes;
} while (bytes != 0 /* peer closed connection */);

printk("Received %d bytes\n", off);

/* Print HTTP response */
p = strstr(recv_buf, "\r\n");
if (p) {
off = p - recv_buf;
recv_buf[off + 1] = '\0';
printk("\n>\t %s\n\n", recv_buf);
}

data_len = snprintf(send_buf,SEND_BUF_SIZE,"%s",HTTP_GET_DETECT);
off = 0;
do {
bytes = send(fd, &send_buf[off], data_len - off, 0);
if (bytes < 0) {
printk("2.send() failed, err %d\n", errno);
goto clean_up;
}
off += bytes;
} while (off < data_len);

printk("2.Sent %d bytes\n", off);

off = 0;
do {
bytes = recv(fd, &recv_buf[off], RECV_BUF_SIZE - off, 0);
if (bytes < 0) {
printk("2.recv() failed, err %d\n", errno);
goto clean_up;
}
off += bytes;
} while (bytes != 0 /* peer closed connection */);

printk("2.Received %d bytes\n", off);

/* Print HTTP response */
p = strstr(recv_buf, "\r\n");
if (p) {
off = p - recv_buf;
recv_buf[off + 1] = '\0';
printk("\n>\t %s\n\n", recv_buf);
}

printk("Finished, closing socket.\n");

clean_up:
freeaddrinfo(res);
(void)close(fd);

lte_lc_power_off();
}

Related