This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

HTTP send errno -1163019603

Dear Community;

I am using this code provided by a Nordic engineer to make some HTTP request in order to use Telegram Bot API. It works fine but after 1-2 days working I am getting an error at this part of the code: 

int num_bytes = send(client_fd, send_buf, send_data_len, 0);
if (num_bytes < 0) {
	printk("send errno: %d\n", errno);
	goto clean_up;
}

This is the error: "send errno: -1163019603"

The problem is that after first time this happens never recovers its normal functionality.

What is causing this error? How can be solved?

Any idea or suggestion is welcomed. Thanks.

Parents
  • /*
     * Copyright (c) 2019 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
     */
    
    #include <zephyr.h>
    #include <net/socket.h>
    #include <stdio.h>
    #include <drivers/uart.h>
    #include <string.h>
    #include <lte_lc.h>
    
    /*#define HTTP_HOST "google.com"*/
    #define HTTP_HOST "api.telegram.org"
    #define HTTP_PORT 443
    #define RECV_BUF_SIZE 8192
    
    char recv_buf[RECV_BUF_SIZE + 1];
    
    void app_http_start(void)
    {
    	struct sockaddr_in local_addr;
    	struct addrinfo *res;
    	struct addrinfo hints;
    
    	hints.ai_flags = 0;
    	hints.ai_family = AF_INET;
    	hints.ai_socktype = SOCK_STREAM;
    	hints.ai_protocol = 0;
    
    	local_addr.sin_family = AF_INET;
    	local_addr.sin_port = htons(HTTP_PORT);
    	local_addr.sin_addr.s_addr = 0;
    
    	printk("HTTP example\n\r");
    
    	int err = getaddrinfo(HTTP_HOST, NULL, &hints, &res);
    	if (err) {
    		printk("getaddrinfo errno %d\n", errno);
    		/* No clean up needed, just return */
    		return;
    	}
    
    	((struct sockaddr_in *)res->ai_addr)->sin_port = htons(HTTP_PORT);
    
    	char send_buf[] =
    		"GET /bot669648316:AAEFvydDWp1fSMbUrx00bBv-9dZptEfu3gU/sendMessage?chat_id=418166911&text=Test HTTP/1.1\r\n"
    		"Host: api.telegram.org\r\n"
    		"Connection: close\r\n\r\n";
    
    	/*char send_buf[] =
    		"GET / HTTP/1.1\r\n"
    		"Host: www.google.com:443\r\n"
    		"Connection: close\r\n\r\n";*/
    
    	int send_data_len = strlen(send_buf);
    
    	int client_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TLS_1_2);
    
    	enum {
    		NONE = 0,
    		OPTIONAL = 1,
    		REQUIRED = 2,
    	};
    
    	int verify = OPTIONAL;
    
    	err = setsockopt(client_fd, SOL_TLS, TLS_PEER_VERIFY, &verify,
    			 sizeof(verify));
    	if (err) {
    		printk("setsockopt err: %d\n", errno);
    	}
    
    	err = connect(client_fd, (struct sockaddr *)res->ai_addr,
    		      sizeof(struct sockaddr_in));
    	if (err > 0) {
    		printk("connect err: %d\n", errno);
    		goto clean_up;
    	}
    
    	int num_bytes = send(client_fd, send_buf, send_data_len, 0);
    	if (num_bytes < 0) {
    		printk("send errno: %d\n", errno);
    		goto clean_up;
    	}
    
    	int tot_num_bytes = 0;
    
    	do {
    		/* TODO: make a proper timeout *
    		 * Current solution will just hang 
    		 * until remote side closes connection */
    		num_bytes = recv(client_fd, recv_buf, RECV_BUF_SIZE, 0);
    		tot_num_bytes += num_bytes;
    
    		if (num_bytes <= 0) {
    			printk("\nrecv errno: %d\n", errno);
    			break;
    		}
    		printk("%s", recv_buf);
    	} while (num_bytes > 0);
    
    	printk("\n\rFinished. Closing socket");
    clean_up:
    	freeaddrinfo(res);
    	err = close(client_fd);
    }
    
    static void modem_configure(void)
    {
    	if (IS_ENABLED(CONFIG_LTE_AUTO_INIT_AND_CONNECT)) {
    		/* Do nothing, modem is already turned on and connected. */
    	} else {
    		int err;
    
    		printk("Establishing LTE link (this may take some time) ...\n");
    		err = lte_lc_init_and_connect();
    		__ASSERT(err == 0, "LTE link could not be established.");
    		printk("LTE connection established.\n");
    	}
    }
    
    int main(void)
    {
    
    	int err1;
    
    	while (1){
    
    	    printf("\nStarting HTTPS test.\n");
    	    modem_configure();
                k_sleep(1000);
    	    app_http_start();
    
    	    if ( (err1 = lte_lc_offline() ) ){
    
    	      printk( "Failure turning off Modem: %d\n", err1 );
    
    	    }
    
    	    else{
    
    
    	      printk("Modem turned off.\n");
    
    	    }
    
    	    k_sleep(1000);
    
    	}
    }
    

    Hello;

    I have made some test with the NRF9160 DK:

    I have updated to v2.1.99-ncs1-rc1 from v1.4.99 I think. After that "-1163019603" Errno disappeared and now I get "22" errno. The only change I have needed to make is to comment "#CONFIG_NET_RAW_MODE=y" line in prj.conf.

    Do we know what does errno "22" mean?

    I will attach here my code to send a Telegram message. Can anyone try to run it? It is not necessary to make any change. If so, the code should send a message to my telegram account.

    Thanks for your support.

Reply
  • /*
     * Copyright (c) 2019 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
     */
    
    #include <zephyr.h>
    #include <net/socket.h>
    #include <stdio.h>
    #include <drivers/uart.h>
    #include <string.h>
    #include <lte_lc.h>
    
    /*#define HTTP_HOST "google.com"*/
    #define HTTP_HOST "api.telegram.org"
    #define HTTP_PORT 443
    #define RECV_BUF_SIZE 8192
    
    char recv_buf[RECV_BUF_SIZE + 1];
    
    void app_http_start(void)
    {
    	struct sockaddr_in local_addr;
    	struct addrinfo *res;
    	struct addrinfo hints;
    
    	hints.ai_flags = 0;
    	hints.ai_family = AF_INET;
    	hints.ai_socktype = SOCK_STREAM;
    	hints.ai_protocol = 0;
    
    	local_addr.sin_family = AF_INET;
    	local_addr.sin_port = htons(HTTP_PORT);
    	local_addr.sin_addr.s_addr = 0;
    
    	printk("HTTP example\n\r");
    
    	int err = getaddrinfo(HTTP_HOST, NULL, &hints, &res);
    	if (err) {
    		printk("getaddrinfo errno %d\n", errno);
    		/* No clean up needed, just return */
    		return;
    	}
    
    	((struct sockaddr_in *)res->ai_addr)->sin_port = htons(HTTP_PORT);
    
    	char send_buf[] =
    		"GET /bot669648316:AAEFvydDWp1fSMbUrx00bBv-9dZptEfu3gU/sendMessage?chat_id=418166911&text=Test HTTP/1.1\r\n"
    		"Host: api.telegram.org\r\n"
    		"Connection: close\r\n\r\n";
    
    	/*char send_buf[] =
    		"GET / HTTP/1.1\r\n"
    		"Host: www.google.com:443\r\n"
    		"Connection: close\r\n\r\n";*/
    
    	int send_data_len = strlen(send_buf);
    
    	int client_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TLS_1_2);
    
    	enum {
    		NONE = 0,
    		OPTIONAL = 1,
    		REQUIRED = 2,
    	};
    
    	int verify = OPTIONAL;
    
    	err = setsockopt(client_fd, SOL_TLS, TLS_PEER_VERIFY, &verify,
    			 sizeof(verify));
    	if (err) {
    		printk("setsockopt err: %d\n", errno);
    	}
    
    	err = connect(client_fd, (struct sockaddr *)res->ai_addr,
    		      sizeof(struct sockaddr_in));
    	if (err > 0) {
    		printk("connect err: %d\n", errno);
    		goto clean_up;
    	}
    
    	int num_bytes = send(client_fd, send_buf, send_data_len, 0);
    	if (num_bytes < 0) {
    		printk("send errno: %d\n", errno);
    		goto clean_up;
    	}
    
    	int tot_num_bytes = 0;
    
    	do {
    		/* TODO: make a proper timeout *
    		 * Current solution will just hang 
    		 * until remote side closes connection */
    		num_bytes = recv(client_fd, recv_buf, RECV_BUF_SIZE, 0);
    		tot_num_bytes += num_bytes;
    
    		if (num_bytes <= 0) {
    			printk("\nrecv errno: %d\n", errno);
    			break;
    		}
    		printk("%s", recv_buf);
    	} while (num_bytes > 0);
    
    	printk("\n\rFinished. Closing socket");
    clean_up:
    	freeaddrinfo(res);
    	err = close(client_fd);
    }
    
    static void modem_configure(void)
    {
    	if (IS_ENABLED(CONFIG_LTE_AUTO_INIT_AND_CONNECT)) {
    		/* Do nothing, modem is already turned on and connected. */
    	} else {
    		int err;
    
    		printk("Establishing LTE link (this may take some time) ...\n");
    		err = lte_lc_init_and_connect();
    		__ASSERT(err == 0, "LTE link could not be established.");
    		printk("LTE connection established.\n");
    	}
    }
    
    int main(void)
    {
    
    	int err1;
    
    	while (1){
    
    	    printf("\nStarting HTTPS test.\n");
    	    modem_configure();
                k_sleep(1000);
    	    app_http_start();
    
    	    if ( (err1 = lte_lc_offline() ) ){
    
    	      printk( "Failure turning off Modem: %d\n", err1 );
    
    	    }
    
    	    else{
    
    
    	      printk("Modem turned off.\n");
    
    	    }
    
    	    k_sleep(1000);
    
    	}
    }
    

    Hello;

    I have made some test with the NRF9160 DK:

    I have updated to v2.1.99-ncs1-rc1 from v1.4.99 I think. After that "-1163019603" Errno disappeared and now I get "22" errno. The only change I have needed to make is to comment "#CONFIG_NET_RAW_MODE=y" line in prj.conf.

    Do we know what does errno "22" mean?

    I will attach here my code to send a Telegram message. Can anyone try to run it? It is not necessary to make any change. If so, the code should send a message to my telegram account.

    Thanks for your support.

Children
Related