# DatagramSocket::read is always blocking (and a fix)

**URL:** <https://forum.juce.com/t/datagramsocket-read-is-always-blocking-and-a-fix/15769>\
**Category:** General JUCE discussion\
**Created:** [October 24, 2015, 9:33pm UTC](https://forum.juce.com/t/datagramsocket-read-is-always-blocking-and-a-fix/15769 "2015-10-24T21:33:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/edy/32/111_2.png) [@Edy](https://forum.juce.com/u/Edy)\
**Post date:** [October 24, 2015, 9:33pm UTC](https://forum.juce.com/t/datagramsocket-read-is-always-blocking-and-a-fix/15769/1 "2015-10-24T21:33:11Z")

</div>

Hello,

I was writting&nbsp;an UDP listener and I faced the problem that the call to **DatagramSocket::read** always blocks&nbsp;the calling thread until data is received, no matter the value of the parameter&nbsp;_blockUntilSpecifiedAmountHasArrived_. If data is not received then the calling thread is blocked forever. If using DatagramSocket::read from a thread's "run" method then that thread will have to be killed the hard way.

Specifically, the **recv** /&nbsp; **recvfrom** calls at the method **readSocket (juce\_Socket.cpp)** block the execution until data is received.

I solved the problem by configuring a timeout for the socket (I found the solution here: http://forums.codeguru.com/showthread.php?248123).

I modified the method **resetSocketOptions** in juce\_Socket.cpp as follows:

```
[EDIT: DISCARDED. See the next post for the correct fix]

static bool resetSocketOptions (const SocketHandle handle, const bool isDatagram, const bool allowBroadcast) noexcept
{
    const int sndBufSize = 65536;
    const int rcvBufSize = 65536;
    const int one = 1; **const int tmOut = 100;** return handle > 0
        && setsockopt (handle, SOL_SOCKET, SO_RCVBUF, (const char*) &rcvBufSize, sizeof (rcvBufSize)) == 0
        && setsockopt (handle, SOL_SOCKET, SO_SNDBUF, (const char*)&sndBufSize, sizeof (sndBufSize)) == 0
        && (isDatagram ? ((! allowBroadcast) || setsockopt (handle, SOL_SOCKET, SO_BROADCAST, (const char*) &one, sizeof (one)) == 0)**&& setsockopt (handle, SOL\_SOCKET, SO\_RCVTIMEO, (const char\*)&tmOut, sizeof (tmOut)) == 0**: (setsockopt (handle, IPPROTO_TCP, TCP_NODELAY, (const char*) &one, sizeof (one)) == 0));
}
```

So when the socket is a datagram socket, it configures a reading timeout of 100 ms.&nbsp;I'm compiling on Windows now, and the fix works for me. I'll have to compile this project for Linux-ARM next week.

I don't know if this is the more convenient method. Another possible&nbsp;solution is&nbsp;to use fcntl for configuring the socket as non-blocking (described here:&nbsp;http://stackoverflow.com/questions/10312535/how-to-unblock-recv-or-recvfrm-function-in-linux-c).

Is there a better fix/workaround for having non-blocking DatagramSocket::read calls? Can be fixed in the JUCE base code?

Thank you!

---

<div class="post-metadata">

**Author:** ![Edy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/edy/32/111_2.png) [@Edy](https://forum.juce.com/u/Edy)\
**Post date:** [October 24, 2015, 10:02pm UTC](https://forum.juce.com/t/datagramsocket-read-is-always-blocking-and-a-fix/15769/2 "2015-10-24T22:02:00Z")

</div>

I've just noticed that juce\_Sockets.cpp already contains a method for setting the block state of a socket: **SocketHelpers::setSocketBlockingState** &nbsp;which uses fcntl. The bug is that this call is missing in&nbsp;DatagramSocket.

I've just appended

```
SocketHelpers::setSocketBlockingState (handle, false);
```

in the constructor of DatagramSocket and now it works perfect.

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [October 26, 2015, 3:38pm UTC](https://forum.juce.com/t/datagramsocket-read-is-always-blocking-and-a-fix/15769/3 "2015-10-26T15:38:10Z")

</div>

Which version of JUCE are you using? If you look at the curren API of the read method in the DatagramSocket [here](http://learn.juce.com/doc/classDatagramSocket.php#aa6d0fb81186112263c99e88b498f58ed), you will see that it has a&nbsp;`blockUntilSpecifiedAmountHasArrived` parameter. That should do what you need.

---

<div class="post-metadata">

**Author:** ![Edy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/edy/32/111_2.png) [@Edy](https://forum.juce.com/u/Edy)\
**Post date:** [October 26, 2015, 4:40pm UTC](https://forum.juce.com/t/datagramsocket-read-is-always-blocking-and-a-fix/15769/4 "2015-10-26T16:40:00Z")

</div>

Thanks for your reply. I'm using JUCE 3.2.0, and Introjucer says that&nbsp;my modules are up to date.

That's why I think it's a bug: even with that parameter being false, the socket blocks the calling thread when reading until some data is received. I debugged the application and it resulted to block in&nbsp; **SocketHelpers::readSocket** (juce\_Socket.cpp), specifically inside&nbsp;the calls to&nbsp; **recv** or **recvfrom**. Thus there's nothing the parameter _blockUntilSpecifiedAmountHasArrived_&nbsp;can do.

Looks like&nbsp;the socket must be put into non-blocking state for it to work in the way expected by JUCE. It seems&nbsp;that part to be missing in DatagramSocket.

I fixed the problem by adding **SocketHelpers::setSocketBlockingState (handle, false);** at the end of the constructor of DatagramSocket, here:

https://github.com/julianstorer/JUCE/blob/master/modules/juce\_core/network/juce\_Socket.cpp#L588

Then&nbsp;it works as expected.

Note that I'm using the socket for reading only. The blocking state&nbsp;might require further&nbsp;considerations when writting.&nbsp;

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [October 26, 2015, 5:20pm UTC](https://forum.juce.com/t/datagramsocket-read-is-always-blocking-and-a-fix/15769/5 "2015-10-26T17:20:40Z")

</div>

Thanks Edy! You are absolutely right. I just&nbsp;fixed this on the latest tip. Please let me know if this works for you.

---

<div class="post-metadata">

**Author:** ![Edy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/edy/32/111_2.png) [@Edy](https://forum.juce.com/u/Edy)\
**Post date:** [October 26, 2015, 6:10pm UTC](https://forum.juce.com/t/datagramsocket-read-is-always-blocking-and-a-fix/15769/6 "2015-10-26T18:10:26Z")

</div>

I've tested your fix and it works like a charm, thank you very much!

---

<div class="post-metadata">

**Author:** ![jaydee](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jaydee](https://forum.juce.com/u/jaydee)\
**Post date:** [October 27, 2015, 8:19pm UTC](https://forum.juce.com/t/datagramsocket-read-is-always-blocking-and-a-fix/15769/7 "2015-10-27T20:19:41Z")

</div>

Sorry ahead of time, i'm not going to be providing too much details!

Commit 166f54f seems to have done something adverse to&nbsp;socket::read. &nbsp; Here is my call(Mac OS)

```
socket->read (buffer, HTTP_MAX_HEADER_SIZE, false);
```

The function(bytesRead) used to return -1 when the connection was done, now it returns 0 all the time.

If I revert juce\_socket to commit 6b66d63 everything works great. &nbsp;I get the bytes come in, I process them, and it returns -1 when done.

The problem for me is line 212

```
if (bytesRead == 0 && blockUntilSpecifiedAmountHasArrived)

                    bytesRead = -1;
```

bytesRead is 0 and blockUntilSpecifiedA... is false, so bytesRead doesn't become -1.

&nbsp;

Suggestions?

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [October 28, 2015, 11:32am UTC](https://forum.juce.com/t/datagramsocket-read-is-always-blocking-and-a-fix/15769/8 "2015-10-28T11:32:50Z")

</div>

Well I think the function is now works correctly, i.e. it doesn't block if no bytes are available and will return that it read zero bytes. Can you take the zero as an indication that the read is done in your case?

---

<div class="post-metadata">

**Author:** ![jaydee](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jaydee](https://forum.juce.com/u/jaydee)\
**Post date:** [October 28, 2015, 1:36pm UTC](https://forum.juce.com/t/datagramsocket-read-is-always-blocking-and-a-fix/15769/9 "2015-10-28T13:36:07Z")

</div>

I can certainly test.. I just know that before the change as the messages came in I would get some bytes then occassionally 0 while it waiting, and when the connection dropped/finished it would return -1, so I coded to that.
