# FSocket GetConnectionState always returns true

**URL:** <https://forums.unrealengine.com/t/fsocket-getconnectionstate-always-returns-true/345349>\
**Category:** Multiplayer & Networking\
**Tags:** Networking, unreal-engine, question\
**Created:** [January 25, 2016, 10:11pm UTC](https://forums.unrealengine.com/t/fsocket-getconnectionstate-always-returns-true/345349 "2016-01-25T22:11:45Z")\
**Posts on this page:** 5\
**Page:** 1

<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, 10:11pm UTC](https://forums.unrealengine.com/t/fsocket-getconnectionstate-always-returns-true/345349/1 "2016-01-25T22:11:45Z")

</div>

When my client disconnects from the server, Unreal never seems to recognize that the disconnect happened in Windows (I have not tried in Linux). It seems like the following conditional should return false when a client disconnects, but this function never returns anything other than SCS\_Connected.

```
 ClientSocket->GetConnectionState() == ESocketConnectionState::SCS_Connected

```

Cobbling together what few examples I can find on FSocket, this is my code below. It successfully detects the client, and receives data, but cannot detect a client disconnect.

**Setting up the server socket:**

```
       Socket = TSharedPtr<FSocket, ESPMode::ThreadSafe>(ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT("SimulatorSocket"), false));

	FIPv4Address ip;
	FIPv4Address::Parse(address, ip);

	TSharedRef<FInternetAddr> addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
	addr->SetIp(ip.GetValue());
	addr->SetPort(port);

	Socket->SetReuseAddr(true);
	Socket->Bind(*addr);
	Socket->Listen(1);

```

**Listening loop:**

```
   TArray<uint8> ReceivedData;
	int32 byteCount = 0;
	uint32 Size = 0;
	bool pending = false;

	while (Server->IsRunning()){
		
		while (Server->IsRunning() && Server->Socket->HasPendingConnection(pending) && pending) {
			
			UE_LOG(LogTemp, Warning, TEXT("Client detected"));

			TSharedRef<FInternetAddr> RemoteAddress = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
			TSharedPtr<FSocket> ClientSocket = TSharedPtr<FSocket>(Server->Socket->Accept(*RemoteAddress, TEXT("HolodeckServerClient")));

			while (Server->IsRunning() && ClientSocket.IsValid() && ClientSocket->GetConnectionState() == ESocketConnectionState::SCS_Connected) {
				
				if (ClientSocket->HasPendingData(Size)) {
					
					ReceivedData.SetNumUninitialized(Size); //FMath::Min(Size, 65507u)
					
					if (ClientSocket->Recv(ReceivedData.GetData(), ReceivedData.Num(), byteCount)) {
						UE_LOG(LogTemp, Warning, TEXT("Success!"));

						// TODO: Parse the data, create a message, publish
						// Server->Publish(new FHolodeckCommand(.5));

					}
					else {
						// Error receiving data, closing the client socket as a precaution
						ClientSocket->Close();
					}
				}
			}

			if (ClientSocket.IsValid()) {
				UE_LOG(LogTemp, Warning, TEXT("Disconnected from client!"));
				ClientSocket->Close();
				//ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(ClientSocket.Get());
				UE_LOG(LogTemp, Warning, TEXT("Closed Socket!"));
			}
		}

	}
```

---

<div class="post-metadata">

**Author:** ![daniel.vuljanic](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@daniel.vuljanic](https://forums.unrealengine.com/u/daniel.vuljanic)\
**Post date:** [February 1, 2016, 10:03am UTC](https://forums.unrealengine.com/t/fsocket-getconnectionstate-always-returns-true/345349/2 "2016-02-01T10:03:24Z")

</div>

Hallo rpotttorff.  
Any success on that issue? Im facing the same problem here.

---

<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:** [February 1, 2016, 1:11pm UTC](https://forums.unrealengine.com/t/fsocket-getconnectionstate-always-returns-true/345349/3 "2016-02-01T13:11:34Z")

</div>

nope - I’m pretty sure it’s an issue/bug… I ended up migrating to ZMQ.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_9194ce9a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_9194ce9a](https://forums.unrealengine.com/u/anonymous_user_9194ce9a)\
**Post date:** [February 26, 2016, 3:06am UTC](https://forums.unrealengine.com/t/fsocket-getconnectionstate-always-returns-true/345349/4 "2016-02-26T03:06:18Z")

</div>

Hi, I have same issue.  
plz let me know if you found solution.

---

<div class="post-metadata">

**Author:** ![TooManyCrashes](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TooManyCrashes](https://forums.unrealengine.com/u/TooManyCrashes)\
**Post date:** [June 15, 2017, 6:19pm UTC](https://forums.unrealengine.com/t/fsocket-getconnectionstate-always-returns-true/345349/5 "2017-06-15T18:19:19Z")

</div>

The bad news is that it is in fact a bug and Epic has given up in trying to solve it. Here is the stagnant public tracker: [Unreal Engine Issues and Bug Tracker (UE-27542)](https://issues.unrealengine.com/issue/UE-27542)

The good news is that other people have written fixes for it. Here is the pull request:  
[https://github.com/EpicGames/UnrealEngine/pull/2882/files](https://github.com/EpicGames/UnrealEngine/pull/2882/files)

Good luck!

Franco
