# When does FSocket block?

**URL:** <https://forums.unrealengine.com/t/when-does-fsocket-block/303677>\
**Category:** C++\
**Tags:** sockets, fsocket, unreal-engine, question, CPP\
**Created:** [January 12, 2015, 12:32am UTC](https://forums.unrealengine.com/t/when-does-fsocket-block/303677 "2015-01-12T00:32:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mattlauk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mattlauk](https://forums.unrealengine.com/u/Mattlauk)\
**Post date:** [January 12, 2015, 12:32am UTC](https://forums.unrealengine.com/t/when-does-fsocket-block/303677/1 "2015-01-12T00:32:52Z")

</div>

Considering ClientSocket is an FSocket, is this ideal for getting data from the socket as quick as possible?

```
while (this->IsAlive)
{
    if (this->ClientSocket->HasPendingData(dataSize) || this->ClientSocket->Wait(ESocketWaitConditions::WaitForRead, FTimespan::FromSeconds(1)))
    {
    }
}

```

I would honestly prefer **this-\>ClientSocket-\>HasPendingData(dataSize)** to be blocking but as far as I can tell, it is not.

---

<div class="post-metadata">

**Author:** ![gmpreussner\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gmpreussner\_1](https://forums.unrealengine.com/u/gmpreussner_1)\
**Post date:** [January 12, 2015, 7:38pm UTC](https://forums.unrealengine.com/t/when-does-fsocket-block/303677/2 "2015-01-12T19:38:09Z")

</div>

_HasPendingData()_ is specifically meant as a polling mechanism to check whether there is data _without_ blocking. If you want to block, just use Wait() by itself. It is like _HasPendingData()_, but blocks. Use one or the other, but not both.

---

<div class="post-metadata">

**Author:** ![Mattlauk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mattlauk](https://forums.unrealengine.com/u/Mattlauk)\
**Post date:** [January 12, 2015, 7:45pm UTC](https://forums.unrealengine.com/t/when-does-fsocket-block/303677/3 "2015-01-12T19:45:49Z")

</div>

What happens when, if it takes a long time before returning to Wait() and between that time, the socket has received more data? Does it wait for the next read or does it know there’s data and continue?

---

<div class="post-metadata">

**Author:** ![gmpreussner\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gmpreussner\_1](https://forums.unrealengine.com/u/gmpreussner_1)\
**Post date:** [January 12, 2015, 7:55pm UTC](https://forums.unrealengine.com/t/when-does-fsocket-block/303677/4 "2015-01-12T19:55:21Z")

</div>

The _FTimespan_ parameter in Wait() is the _maximum_ time to wait. If you call it with _WaitForRead_, it will unblock as soon as data is available for reading. It will also unblock if the specified timeout is exceeded.

So in your example code, the Wait() will block until there is either data ready for reading, or if one second has passed, whichever comes first.

---

<div class="post-metadata">

**Author:** ![Mattlauk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mattlauk](https://forums.unrealengine.com/u/Mattlauk)\
**Post date:** [January 12, 2015, 8:09pm UTC](https://forums.unrealengine.com/t/when-does-fsocket-block/303677/5 "2015-01-12T20:09:08Z")

</div>

Thanks for answering. I wasn’t sure if the socket had pending data and Wait() was called if it would wait until the socket received more data, or if it wouldn’t wait and continue.

I would like to look at the socket source for windows. Would you by chance know the subclass/es name/s?

---

<div class="post-metadata">

**Author:** ![gmpreussner\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gmpreussner\_1](https://forums.unrealengine.com/u/gmpreussner_1)\
**Post date:** [January 12, 2015, 8:21pm UTC](https://forums.unrealengine.com/t/when-does-fsocket-block/303677/6 "2015-01-12T20:21:01Z")

</div>

Windows uses BSD sockets now, which are implemented in FSocketBSD (/Runtime/Sockets/Private/BSDSockets/SocketsBSD.h).

---

<div class="post-metadata">

**Author:** ![Mattlauk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mattlauk](https://forums.unrealengine.com/u/Mattlauk)\
**Post date:** [January 12, 2015, 8:23pm UTC](https://forums.unrealengine.com/t/when-does-fsocket-block/303677/7 "2015-01-12T20:23:40Z")

</div>

Thanks! (This is here because I needed more character to say thanks.)
