# Non blocking socket design: FreeRTOS\_FD\_ISSET and read/write/exception event granularity

**URL:** <https://forums.freertos.org/t/non-blocking-socket-design-freertos-fd-isset-and-read-write-exception-event-granularity/7833>\
**Category:** Kernel\
**Created:** [May 6, 2019, 10:21pm UTC](https://forums.freertos.org/t/non-blocking-socket-design-freertos-fd-isset-and-read-write-exception-event-granularity/7833 "2019-05-06T22:21:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 6, 2019, 10:21pm UTC](https://forums.freertos.org/t/non-blocking-socket-design-freertos-fd-isset-and-read-write-exception-event-granularity/7833/1 "2019-05-06T22:21:23Z")

</div>

**[willschmid](https://sourceforge.net/u/willschmid/)** wrote on [Monday, May 06, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/d09dcec26c/#b176):

As part of my non blocking (asynchronous) socket application, I need to know what type of event FreeRTOS\_select generated for a particular socket.

FreeRTOS\_FD\_ISSET simply returns true if any event flag is set. How can I find out what type of event was generated for the socket?

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [May 7, 2019, 5:52am UTC](https://forums.freertos.org/t/non-blocking-socket-design-freertos-fd-isset-and-read-write-exception-event-granularity/7833/2 "2019-05-07T05:52:58Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Tuesday, May 07, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/d09dcec26c/#d5ca):

Willhelm, the code in `FreeRTOS_FD_ISSET()` can be read as follows:

if `xSocket` is part of ( connected to ) `xSocketSet`, the select event mask in `xSocketBits` will be returned.

This mask has 4 bits, more or less comparable to the BSD `select()` :

```auto
    eSELECT_READ /* the socket has data in its RX buffer */
    eSELECT_WRITE /* the socket has space in its TX buffer */
    eSELECT_EXCEPT /* the socket has had an exception (connection closed) */
    eSELECT_INTR /* and API call got interrupted after FreeRTOS_SignalSocket() was called. */

```

`eSELECT_READ` has a second use: when a socket in listening mode receives a new connection, the event is generated.  
Likewise, `eSELECT_WRITE` is generated as soon as `connect()` leads to a TCP connection.

So for listening “server” sockets, it is important to enable `eSELECT_READ`, and for a connecting “client” sockets, it is recommended to enable the `eSELECT_WRITE` event.

When `FreeRTOS_send()` returns less bytes than expected, or the error `-pdFREERTOS_ERRNO_ENOSPC`, it is good to enable `eSELECT_WRITE` for that socket, and get woken up as soon as there is space.  
But `eSELECT_WRITE` can be nasty: when it occurs you either send data, or disable it. If not, it will come back again and again. Nothing peculiar here: the same behaviour can be expected on a PC.

Inheritance: when a server socket receives a new connection, the new connection will automatically belong the the same select group, and it will have the `eSELECT_READ` and `eSELECT_EXCEPT` bits set.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 7, 2019, 3:48pm UTC](https://forums.freertos.org/t/non-blocking-socket-design-freertos-fd-isset-and-read-write-exception-event-granularity/7833/3 "2019-05-07T15:48:30Z")

</div>

**[willschmid](https://sourceforge.net/u/willschmid/)** wrote on [Tuesday, May 07, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/d09dcec26c/#38b5):

Thank you for your detailed explanation Hein. I am looking at the documentation [https://www.freertos.org/FreeRTOS-Plus/FreeRTOS\_Plus\_TCP/API/FD\_ISSET.html](https://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/API/FD_ISSET.html) and I am not sure how I missed reading the details.
