<?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>I am not able to create socket for TCP communication</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/118531/i-am-not-able-to-create-socket-for-tcp-communication</link><description>Hello evebody, 
 
 I have problem with socket creation. I am trying to copy example of this tutorial: 
 https://academy.nordicsemi.com/courses/cellular-iot-fundamentals/lessons/lesson-3-cellular-fundamentals/topic/lesson-3-exercise-1/ 
 What is my idea</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 04 Feb 2025 12:27:47 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/118531/i-am-not-able-to-create-socket-for-tcp-communication" /><item><title>RE: I am not able to create socket for TCP communication</title><link>https://devzone.nordicsemi.com/thread/521298?ContentTypeID=1</link><pubDate>Tue, 04 Feb 2025 12:27:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9d4707c9-da32-46f7-8582-c9a910e09df3</guid><dc:creator>dejans</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;Thank you for the update.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Dejan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I am not able to create socket for TCP communication</title><link>https://devzone.nordicsemi.com/thread/521176?ContentTypeID=1</link><pubDate>Mon, 03 Feb 2025 16:49:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ae519c02-c35a-445a-953e-dc7c48555d80</guid><dc:creator>Jaroslav Havel</dc:creator><description>&lt;pre id="tw-target-text" class="tw-data-text tw-text-large tw-ta" dir="ltr" data-ved="2ahUKEwjx_vK5-qeLAxVW1QIHHTXDBT4Q3ewLegQIBxAV"&gt;&lt;span class="Y2IQFc" lang="en"&gt;Thank you for your interest, I have already managed to remove the error. The problem was in the registration to the network of the communication intermediary.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Regards&lt;br /&gt;Jaroslav&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I am not able to create socket for TCP communication</title><link>https://devzone.nordicsemi.com/thread/521175?ContentTypeID=1</link><pubDate>Mon, 03 Feb 2025 16:49:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:557169a9-8251-4dde-84f6-704864ea5b6c</guid><dc:creator>Jaroslav Havel</dc:creator><description>&lt;pre id="tw-target-text" class="tw-data-text tw-text-large tw-ta" dir="ltr" data-ved="2ahUKEwjx_vK5-qeLAxVW1QIHHTXDBT4Q3ewLegQIBxAV"&gt;&lt;span class="Y2IQFc" lang="en"&gt;Thank you for your interest, I have already managed to remove the error. The problem was in the registration to the network of the communication intermediary.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Regards&lt;br /&gt;Jaroslav&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I am not able to create socket for TCP communication</title><link>https://devzone.nordicsemi.com/thread/521173?ContentTypeID=1</link><pubDate>Mon, 03 Feb 2025 16:42:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e538ba2f-bad7-490c-83a5-1d0121e394bb</guid><dc:creator>dejans</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;Could you provide complete application log which shows the &amp;quot;Network is unreachable&amp;quot; error?&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Dejan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I am not able to create socket for TCP communication</title><link>https://devzone.nordicsemi.com/thread/521152?ContentTypeID=1</link><pubDate>Mon, 03 Feb 2025 14:28:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8bc852b4-c6be-4852-9883-38e5610fdb26</guid><dc:creator>Eivind J</dc:creator><description>&lt;p&gt;Hi, have you made sure that the modem is connected to the network?&amp;nbsp;&lt;br /&gt;&lt;br /&gt;You can use the &lt;a href="https://docs.nordicsemi.com/bundle/ncs-2.9.0/page/nrf/libraries/modem/lte_lc.html"&gt;LTE link controller&lt;/a&gt; library for this, or to do it manually with use of the AT monitor library (CONFIG_AT_MONITOR=y):&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;K_SEM_DEFINE(cereg_sem, 0, 1);
AT_MONITOR(at_mon_cereg, &amp;quot;+CEREG&amp;quot;, at_handler_cereg);

static void at_handler_cereg(const char *notif)
{
	int err;

	printk(&amp;quot;+CEREG notification: %.*s\n&amp;quot;, strlen(notif) - strlen(&amp;quot;\r\n&amp;quot;), notif);

	if (!strncmp(notif, &amp;quot;+CEREG: 1&amp;quot;, strlen(&amp;quot;+CEREG: 1&amp;quot;)) ||
	    !strncmp(notif, &amp;quot;+CEREG: 5&amp;quot;, strlen(&amp;quot;+CEREG: 5&amp;quot;))) {
	    /* Registered to home network or roaming */
		k_sem_give(&amp;amp;cereg_sem);
	}
}

static int network_cfun(unsigned int mode)
{
	int err;

	err = nrf_modem_at_printf(&amp;quot;AT+CFUN=%u&amp;quot;, mode);
	if (err) {
		printk(&amp;quot;nrf_modem_at_printf failed, err %d\n&amp;quot;, err);
		return err;
	}

	if (mode == 1 || mode == 21) {
		err = k_sem_take(&amp;amp;cereg_sem, K_MSEC(4000));
		if (err) {
			printk(&amp;quot;Failed to register to network, err %d\n&amp;quot;, err);
		}
	}
}

In main: 

/* Subscribe to network registration status notifications */
err = nrf_modem_at_printf(&amp;quot;AT+CEREG=1&amp;quot;);
if (err) {
	printk(&amp;quot;Failed to register to network status notifications, err %d\n&amp;quot;, err);
	return err;
}

/* Connect to network */
err = network_cfun(1);
if (err) {
	printk(&amp;quot;Functional mode change failed, err %d\n&amp;quot;, err);
	return err;
}

/* Create socket and connect */ 
...&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I am not able to create socket for TCP communication</title><link>https://devzone.nordicsemi.com/thread/521005?ContentTypeID=1</link><pubDate>Sun, 02 Feb 2025 17:45:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:44f8aea9-41a8-4f5a-9a95-ffc80b93f0c5</guid><dc:creator>Jaroslav Havel</dc:creator><description>&lt;p&gt;Thank you for reply, now I am able to create socket &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f60a.svg" title="Blush"&gt;&amp;#x1f60a;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;But I have another problem, if i try to connect my client to TCP server (I am able to establish connection with TCP server running on local IP using NAT, when&amp;nbsp;I use Serial Console, AT command mode), using function &lt;strong&gt;&lt;span style="background-color:#ff0000;"&gt;connect&lt;/span&gt;&lt;/strong&gt; I am not able to establish communication.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(64999);  // Port serveru
inet_pton(AF_INET, &amp;quot;98.58.85.213&amp;quot;, &amp;amp;server_addr.sin_addr);
int sock;
sock= socket(server_addr.sin_family, SOCK_STREAM, IPPROTO_TCP);
	           if (sock &amp;lt; 0) 
             {
		            printk(&amp;quot;Failed to create TCP socket, error: %d\n&amp;quot;, errno);

	           }
             else 
             {
                printk(&amp;quot;SOCKET created, fine\n&amp;quot;);
                char ip_str[INET_ADDRSTRLEN];  
                inet_ntop(AF_INET, &amp;amp;server_addr.sin_addr, ip_str, sizeof(ip_str));
                printk(&amp;quot;IP adress of server is: %s\n&amp;quot;, ip_str);
                vytvoreno=true;
 

             } 
          // after creating socket with some delay
          int ret=connect(sock, (struct sockaddr *)&amp;amp;server_addr, sizeof(server_addr));
          if(ret==0)
          {
             printf(&amp;quot;Podarilo se pripojit\n&amp;quot;);
             pripojeno=true;

          }
          else
          {
              printf(&amp;quot;Nepodarilo se pripojit: %d\n&amp;quot;, ret);
              printf(&amp;quot;Chyba pri vytvareni socketu: %d (%s)\n&amp;quot;, errno, strerror(errno));

          }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I get error: 114 (Network is unreachable), but i know that is reachable, i tested this with AT commands.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Some tip what to do?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I am not able to create socket for TCP communication</title><link>https://devzone.nordicsemi.com/thread/520976?ContentTypeID=1</link><pubDate>Sat, 01 Feb 2025 15:09:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:452bba85-7981-4806-b104-1695f907346e</guid><dc:creator>Eivind J</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Removing CONFIG_NET_L2_PPP=y and CONFIG_NET_DHCPV4=y should make your code compile. It is not needed to use the modem library and the nRF modem. If you need these for other reasons you can enable CONFIG_NET_NATIVE, which is disabled in&amp;nbsp;your prj.conf.&lt;br /&gt;The following should for instance be enough in many cases:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;# Network
CONFIG_NETWORKING=y
CONFIG_NET_NATIVE=n
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_OFFLOAD=y
CONFIG_NET_IPV4=y
CONFIG_NET_IPV6=y

# Modem library
CONFIG_NRF_MODEM_LIB=y&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t see any immediate issues with the device tree provided. The&amp;nbsp;default device tree in zephyr should also be ok. Note that if you want to add changes to the nRF916DK device tree for your specific application,&amp;nbsp;you can add a device tree overlay with what you need in your application folder, in &amp;quot;&amp;lt;your_app_path&amp;gt;/boards/nrf9161dk_nrf9161_ns.overlay&amp;quot;. This is done in some of the cellular samples in NCS, for instance the &lt;a href="https://github.com/nrfconnect/sdk-nrf/blob/main/samples/cellular/modem_trace_flash/boards/nrf9161dk_nrf9161_ns.overlay"&gt;modem trace flash sample&lt;/a&gt;. With this name the file is picked up automatically by the build system.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>