<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How to set a udp socket peer connect timeout?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/92085/how-to-set-a-udp-socket-peer-connect-timeout</link><description>Using the latest nRF_Connect_SDK 
 How can we set a udp/dtls socket, peer connect timeout for this call: 
 
 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.</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 20 Sep 2022 19:58:53 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/92085/how-to-set-a-udp-socket-peer-connect-timeout" /><item><title>RE: How to set a udp socket peer connect timeout?</title><link>https://devzone.nordicsemi.com/thread/387166?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2022 19:58:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:da80ef26-df8b-4136-8cbd-49e16da5d64e</guid><dc:creator>Ron Segal</dc:creator><description>&lt;p&gt;Hi Sigurd&lt;/p&gt;
&lt;p&gt;Yes, bearing in mind that this is a dtls link (as mentioned above, although the question should probably have been titled that), connect&amp;nbsp; (zsock_connect) is blocking.&amp;nbsp; This is clear because the print diagnostics in the code above is never reached until the call eventually times out.&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;However, by implementing the SO_SNDTIMEO socket option that you mentioned, the dtls connect is now timing out at the set number of seconds.&amp;nbsp; Here&amp;#39;s code that works:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;	struct timeval timeo = {
		.tv_sec = 5,
		.tv_usec = 0,
	};

	err = setsockopt(client_fd, SOL_SOCKET, SO_SNDTIMEO, &amp;amp;timeo, sizeof(timeo));
	if (err) {
		DBG_PRINTF(&amp;quot;Failed to set socket timeout, errno %d&amp;quot;, errno);
		err = -errno;
		goto error;
	}

	DBG_PRINTF(&amp;quot;Trying to connect to: &amp;quot;);
#ifdef CONFIG_PRINT_DBG
	printhex(host_addr.data,sizeof(host_addr.data));
#endif
	DBG_PRINTF(&amp;quot;\r\n&amp;quot;);

	//** 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 *)&amp;amp;host_addr, sizeof(struct sockaddr_in));
	if (err &amp;lt; 0) {
		printk(&amp;quot;Connect failed : %d\n&amp;quot;, errno);
		goto error;
	}
	else {
		DBG_PRINTF(&amp;quot;Server Connected\r\n&amp;quot;);
	}

	return 0;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;So problem solved. Thanks for your help in putting me on the right track.&lt;/p&gt;
&lt;p&gt;Cheers, Ron&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to set a udp socket peer connect timeout?</title><link>https://devzone.nordicsemi.com/thread/387087?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2022 13:09:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:be441893-039c-4973-9c3a-31d76fff6fcc</guid><dc:creator>Sigurd Hellesvik</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;After looking around myself for another setting or socket option, I asked our developers.&lt;/p&gt;
&lt;p&gt;Here is what they say:&lt;/p&gt;
&lt;p&gt;&amp;quot;No connection is established for connection-less sockets upon connect(). That will only set the peer address. Therefore connect() on a UDP socket is always a blocking operation. We do support timeouts on connect() for TCP sockets via SO_SNDTIMEO&amp;quot;&lt;/p&gt;
&lt;p&gt;EDIT: Are you certain that it is connect() that is blocking, and not for example send()?&lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;Sigurd Hellesvik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to set a udp socket peer connect timeout?</title><link>https://devzone.nordicsemi.com/thread/387002?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2022 07:44:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:86732eeb-a743-4816-871c-1f481012638a</guid><dc:creator>Ron Segal</dc:creator><description>&lt;p&gt;Hi Sigurd,&lt;/p&gt;
&lt;p&gt;Thanks.&amp;nbsp; I had already tried&amp;nbsp;&lt;span&gt;&amp;nbsp; NET_SOCKETS_CONNECT_TIMEOUT in prj.conf but it doesn&amp;#39;t seem to do anything. The socket still hangs for a long timeout if there is no response from the other end.&amp;nbsp; Unlike&amp;nbsp;CONFIG_LTE_NETWORK_TIMEOUT which does timeout the LTE/NB-IoT connection.&amp;nbsp; Any other ideas.&amp;nbsp; Perhaps the code that is supposed to use that net sockets timeout configuration value can itself be modified?&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to set a udp socket peer connect timeout?</title><link>https://devzone.nordicsemi.com/thread/386997?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2022 07:27:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e7df32db-83f9-4e21-a117-eb19c5c29355</guid><dc:creator>Sigurd Hellesvik</dc:creator><description>&lt;p&gt;Hi Ron,&lt;/p&gt;
&lt;p&gt;I have not tried it myself, but I think you are looking for the &lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/e81870b779762f577ea0f3ba058d446d10eaf5ac/subsys/net/lib/sockets/Kconfig#L48"&gt;NET_SOCKETS_CONNECT_TIMEOUT&lt;/a&gt; configuration option.&lt;/p&gt;
&lt;p&gt;Is this what you needed?&lt;/p&gt;
&lt;p&gt;(It does not appear in our &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.1.0/kconfig/index.html"&gt;Kconfig search&lt;/a&gt; for some reason. I will ask our documentation team about this.)&lt;br /&gt;EDIT: nvm, I found it at &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.1.0/kconfig/index.html#CONFIG_NET_SOCKETS_CONNECT_TIMEOUT"&gt;https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.1.0/kconfig/index.html#CONFIG_NET_SOCKETS_CONNECT_TIMEOUT&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;Sigurd Hellesvik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to set a udp socket peer connect timeout?</title><link>https://devzone.nordicsemi.com/thread/386983?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2022 04:59:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:41f8ea31-5d03-4861-ba04-d57ee66ffda4</guid><dc:creator>Ron Segal</dc:creator><description>&lt;p&gt;Thank you Sigurd.&lt;/p&gt;
&lt;p&gt;There is a dtls to mqtt gateway at the other end of the link, which is also communicating over NB-IoT.&amp;nbsp; If the gateway isn&amp;#39;t working properly or is unreachable for some reason, the client needs to timeout quickly to avoid unnecessary current draw from the battery before trying again.&amp;nbsp;So far I haven&amp;#39;t been able to find a reasonably straightforward way to either timeout or gracefully abort the socket.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to set a udp socket peer connect timeout?</title><link>https://devzone.nordicsemi.com/thread/386926?ContentTypeID=1</link><pubDate>Mon, 19 Sep 2022 13:57:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4e1ebd9b-dc87-4b9c-82e3-5b4918e2c6b4</guid><dc:creator>Sigurd Hellesvik</dc:creator><description>&lt;p&gt;Hi Ron,&lt;/p&gt;
&lt;p&gt;I will look into your case and return with more information tomorrow.&lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;Sigurd Hellesvik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>