<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Detecting a remote disconnection on a TCP socket in MQX Software Solutions</title>
    <link>https://community.nxp.com/t5/MQX-Software-Solutions/Detecting-a-remote-disconnection-on-a-TCP-socket/m-p/192328#M3829</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Typically in an RTOS system the recv() call is blocking, so if the connection is established and no new data arrived, the task is just blocked in the recv() call. &lt;/P&gt;&lt;P&gt;In RTCS, recv() returns -1 (RTCS_ERROR) when remote peer disconnects (sends FIN to us). The socket error code is set to RTCSERR_TCP_CONN_CLOSING and you can read it from the socket by RTCS_geterror().&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Martin&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 21 Apr 2015 06:09:22 GMT</pubDate>
    <dc:creator>Martin_</dc:creator>
    <dc:date>2015-04-21T06:09:22Z</dc:date>
    <item>
      <title>Detecting a remote disconnection on a TCP socket</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Detecting-a-remote-disconnection-on-a-TCP-socket/m-p/192325#M3826</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm starting to use MQX and I am at a loss about the procedure to detect a remote disconnection once &amp;nbsp;a TCP link has been established.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As far as I understood you can establish a connection by either actively calling connect(), or by calling listen() then accept(), once the connection has been established anytime a data packet arrives addressed to that socket it can be detected by pooling&amp;nbsp;RTCS_selectall(0).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My problem is that I haven't found a way to identify when these events are generated by a remote disconnection or by data arriving.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code snippet of my current procedure:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;for (;;) {   sock = RTCS_selectall(0);   if (sock == listensock) {      // Connection requested; accept it.       rlen = sizeof(raddr);      tsock = accept(listensock, &amp;amp;raddr, &amp;amp;rlen);      if (tsock == RTCS_SOCKET_ERROR) {          printf("\naccept() failed, error 0x%lx",                  RTCS_geterror(listensock));          continue;      }   }   else if (sock == tsock) {      // Sends back a message and increments message index      recv(tsock, buffer, 256, 0);      send(tsock, Msg[index], strlen(Quotes[index]) + 1, 0);      index++;   }}&lt;/PRE&gt;&lt;P&gt;Once I remotely disconnect tsock, it keeps returning as a result of RTCS_selectall(0), but as I am unable to identify this is not an incoming message I keep executing the recv/send routine unaware that the socket is broken by the remote peer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to both know the correct procedure to detect such a disconnection as well as the correct procedure to close the socket gracefully in this case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Frederico&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:44:47 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Detecting-a-remote-disconnection-on-a-TCP-socket/m-p/192325#M3826</guid>
      <dc:creator>FredericoPrado</dc:creator>
      <dc:date>2020-10-29T09:44:47Z</dc:date>
    </item>
    <item>
      <title>Re: Detecting a remote disconnection on a TCP socket</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Detecting-a-remote-disconnection-on-a-TCP-socket/m-p/192326#M3827</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't know MQX, but typically with socket programming a closed connection is signaled by recv returning 0 as number of bytes read. There is usually also no guarantee of how many bytes were read in a single recv, it may return with fewer than the passed in buffer size.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Daniel&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Apr 2010 13:09:04 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Detecting-a-remote-disconnection-on-a-TCP-socket/m-p/192326#M3827</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2010-04-15T13:09:04Z</dc:date>
    </item>
    <item>
      <title>Re: Detecting a remote disconnection on a TCP socket</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Detecting-a-remote-disconnection-on-a-TCP-socket/m-p/192327#M3828</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;BR /&gt;If this is true, how do I distinguish between a closed connection and a connection that is still open but no new data arrived?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 20 Apr 2015 17:43:55 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Detecting-a-remote-disconnection-on-a-TCP-socket/m-p/192327#M3828</guid>
      <dc:creator>martinpi</dc:creator>
      <dc:date>2015-04-20T17:43:55Z</dc:date>
    </item>
    <item>
      <title>Re: Detecting a remote disconnection on a TCP socket</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Detecting-a-remote-disconnection-on-a-TCP-socket/m-p/192328#M3829</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Typically in an RTOS system the recv() call is blocking, so if the connection is established and no new data arrived, the task is just blocked in the recv() call. &lt;/P&gt;&lt;P&gt;In RTCS, recv() returns -1 (RTCS_ERROR) when remote peer disconnects (sends FIN to us). The socket error code is set to RTCSERR_TCP_CONN_CLOSING and you can read it from the socket by RTCS_geterror().&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Martin&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Apr 2015 06:09:22 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Detecting-a-remote-disconnection-on-a-TCP-socket/m-p/192328#M3829</guid>
      <dc:creator>Martin_</dc:creator>
      <dc:date>2015-04-21T06:09:22Z</dc:date>
    </item>
    <item>
      <title>Re: Detecting a remote disconnection on a TCP socket</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/Detecting-a-remote-disconnection-on-a-TCP-socket/m-p/192329#M3830</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;From Martin to Martin:&lt;/P&gt;&lt;P&gt;Thanks a lot! :smileyhappy:&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Apr 2015 12:56:19 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/Detecting-a-remote-disconnection-on-a-TCP-socket/m-p/192329#M3830</guid>
      <dc:creator>martinpi</dc:creator>
      <dc:date>2015-04-21T12:56:19Z</dc:date>
    </item>
  </channel>
</rss>

