# How can I check socket state?

**URL:** <https://forums.unrealengine.com/t/how-can-i-check-socket-state/299700>\
**Category:** C++\
**Tags:** Networking, socket, unreal-engine, UE4, question, CPP\
**Created:** [November 23, 2014, 9:06pm UTC](https://forums.unrealengine.com/t/how-can-i-check-socket-state/299700 "2014-11-23T21:06:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_cebf9e9e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_cebf9e9e](https://forums.unrealengine.com/u/anonymous_user_cebf9e9e)\
**Post date:** [November 23, 2014, 9:06pm UTC](https://forums.unrealengine.com/t/how-can-i-check-socket-state/299700/1 "2014-11-23T21:06:06Z")

</div>

This is the code to create and connection socket:

```
Socket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT("ServicesSocket"), false);
TSharedRef<FInternetAddr> Addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr(Ip, Port);
if(!Socket->Connect(*Addr))
{
...
}

```

Inside the Tick function I check the data on the sending and receiving:

```
if (Socket == NULL || Socket->GetConnectionState() != ESocketConnectionState::SCS_Connected))
{
	// do something
}

SendBuf();
Recv();

```

But even if the connection is in fact already closed by the server, the _Socket-\>GetConnectionState()! = ESocketConnectionState::SCS\_Connected_ always true.

How can I check socket state? Thank you.

---

<div class="post-metadata">

**Author:** ![Ryan\_Gerleve](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ryan\_Gerleve](https://forums.unrealengine.com/u/Ryan_Gerleve)\
**Post date:** [November 24, 2014, 4:56pm UTC](https://forums.unrealengine.com/t/how-can-i-check-socket-state/299700/2 "2014-11-24T16:56:07Z")

</div>

Hello,

In standard BSD sockets, if the recv() call returns 0, this indicates the connection was closed gracefully. Currently our wrapper function, FSocketBSD::Recv(), only returns a bool, and doesn’t provide this information, either directly or through GetConnectionState(). We’re now tracking a task to expose this information.

In the meantime, you can work around this in a couple ways:

1. Modify the Recv() function to  
return an int instead of a bool, return the result of the BSD recv() directly, and check that, or
2. If you know for sure your socket is an FSocketBSD, you can use GetNativeSocket(), call recv() directly, and check the return value. Note that this will not set the internal LastActivityTime used in GetConnectionState().

---

<div class="post-metadata">

**Author:** ![ruohki](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ruohki](https://forums.unrealengine.com/u/ruohki)\
**Post date:** [April 3, 2015, 12:40pm UTC](https://forums.unrealengine.com/t/how-can-i-check-socket-state/299700/3 "2015-04-03T12:40:31Z")

</div>

Any progress on this?

---

<div class="post-metadata">

**Author:** ![GerardD\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GerardD\_1](https://forums.unrealengine.com/u/GerardD_1)\
**Post date:** [June 12, 2015, 3:18am UTC](https://forums.unrealengine.com/t/how-can-i-check-socket-state/299700/4 "2015-06-12T03:18:33Z")

</div>

I would also like to know if GetConnectionState has been updated as yet?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_df4ef0f6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_df4ef0f6](https://forums.unrealengine.com/u/anonymous_user_df4ef0f6)\
**Post date:** [July 7, 2015, 1:47pm UTC](https://forums.unrealengine.com/t/how-can-i-check-socket-state/299700/5 "2015-07-07T13:47:31Z")

</div>

Maybe it will be useful

Maybe looks strange, but works (at least in windows)

{

FSocket\* m\_Socket;

…

…

TArray ReceivedData;

uint32 Size;

while (m\_Socket-\>HasPendingData(Size))

{

```
int32 Read = 0;

m_Socket->Recv(ReceivedData.GetData(), ReceivedData.Num(), Read);

```

}

bool HasPendingConnection = false;

bool Res = m\_Socket-\>HasPendingConnection(HasPendingConnection);

}

in case has connection : Res == true & HasPendingConnection == FALSE

in case hasn’t connection : Res == true & HasPendingConnection == TRUE

important: call HasPendingConnection() after Recv()

---

<div class="post-metadata">

**Author:** ![mborromeo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mborromeo](https://forums.unrealengine.com/u/mborromeo)\
**Post date:** [August 27, 2015, 1:04pm UTC](https://forums.unrealengine.com/t/how-can-i-check-socket-state/299700/6 "2015-08-27T13:04:54Z")

</div>

+1  
What’s the status of this method? Is it reliable or not?

---

<div class="post-metadata">

**Author:** ![rpottorff](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@rpottorff](https://forums.unrealengine.com/u/rpottorff)\
**Post date:** [January 25, 2016, 9:51pm UTC](https://forums.unrealengine.com/t/how-can-i-check-socket-state/299700/7 "2016-01-25T21:51:18Z")

</div>

+1 Any status update?

---

<div class="post-metadata">

**Author:** ![hiili](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hiili](https://forums.unrealengine.com/u/hiili)\
**Post date:** [April 12, 2016, 7:23pm UTC](https://forums.unrealengine.com/t/how-can-i-check-socket-state/299700/8 "2016-04-12T19:23:53Z")

</div>

It seems that this issue makes it quite difficult if not impossible to use the socket subsystem in a production-ready way (robust and portable) without switching to a source build. At the same time, it seems that fixing it would be really trivial for an Epic staff member?

This issue has been open for over a year now. Any chance in bumping its priority a bit?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_59c8ac2e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_59c8ac2e](https://forums.unrealengine.com/u/anonymous_user_59c8ac2e)\
**Post date:** [September 21, 2016, 9:27pm UTC](https://forums.unrealengine.com/t/how-can-i-check-socket-state/299700/9 "2016-09-21T21:27:05Z")

</div>

I’m also encountering this and would like to see it fixed.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_59c8ac2e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_59c8ac2e](https://forums.unrealengine.com/u/anonymous_user_59c8ac2e)\
**Post date:** [September 21, 2016, 10:16pm UTC](https://forums.unrealengine.com/t/how-can-i-check-socket-state/299700/10 "2016-09-21T22:16:28Z")

</div>

Also, I should mention that I don’t see a way to make Recv non-blocking, so in order to use its return value, I’d need to be okay with blocking. I think there should be a non-blocking method of checking whether the connection is closed.

---

<div class="post-metadata">

**Author:** ![ucrmk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ucrmk](https://forums.unrealengine.com/u/ucrmk)\
**Post date:** [November 14, 2017, 10:25am UTC](https://forums.unrealengine.com/t/how-can-i-check-socket-state/299700/11 "2017-11-14T10:25:59Z")

</div>

thank you !! it works!!

---

<div class="post-metadata">

**Author:** ![Editor69](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Editor69](https://forums.unrealengine.com/u/Editor69)\
**Post date:** [February 24, 2022, 4:46pm UTC](https://forums.unrealengine.com/t/how-can-i-check-socket-state/299700/12 "2022-02-24T16:46:37Z")

</div>

+1, Any progress on this?
