How to set a udp socket peer connect timeout?

Using the latest nRF_Connect_SDK

How can we set a udp/dtls socket, peer connect timeout for this call:

//** zsock_connect - connect socket to peer network
	//** https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.0.0/zephyr/connectivity/networking/api/sockets.html
	err = connect(client_fd, (struct sockaddr *)&host_addr, sizeof(struct sockaddr_in));
	if (err < 0) {
		printk("Connect failed : %d\n", errno);
		goto error;
	}
	else {
		DBG_PRINTF("Server Connected\r\n");
	}

If there is no way to set a timeout for the call itself, is there a way to abort the call? Then a separate timeout can be set.

Thanks.

Parents Reply
  • Hi Sigurd

    Yes, bearing in mind that this is a dtls link (as mentioned above, although the question should probably have been titled that), connect  (zsock_connect) is blocking.  This is clear because the print diagnostics in the code above is never reached until the call eventually times out.  

    However, by implementing the SO_SNDTIMEO socket option that you mentioned, the dtls connect is now timing out at the set number of seconds.  Here's code that works:

    	struct timeval timeo = {
    		.tv_sec = 5,
    		.tv_usec = 0,
    	};
    
    	err = setsockopt(client_fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo));
    	if (err) {
    		DBG_PRINTF("Failed to set socket timeout, errno %d", errno);
    		err = -errno;
    		goto error;
    	}
    
    	DBG_PRINTF("Trying to connect to: ");
    #ifdef CONFIG_PRINT_DBG
    	printhex(host_addr.data,sizeof(host_addr.data));
    #endif
    	DBG_PRINTF("\r\n");
    
    	//** zsock_connect - connect socket to dtls peer - our udp2mqtt gateway
    	//** https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.0.0/zephyr/connectivity/networking/api/sockets.html
    	err = connect(client_fd, (struct sockaddr *)&host_addr, sizeof(struct sockaddr_in));
    	if (err < 0) {
    		printk("Connect failed : %d\n", errno);
    		goto error;
    	}
    	else {
    		DBG_PRINTF("Server Connected\r\n");
    	}
    
    	return 0;
    

    So problem solved. Thanks for your help in putting me on the right track.

    Cheers, Ron

Children
No Data
Related