# FSocket doesn't stay connected

**URL:** <https://forums.unrealengine.com/t/fsocket-doesnt-stay-connected/343776>\
**Category:** Multiplayer & Networking\
**Tags:** Networking, fsocket, unreal-engine, question, CPP\
**Created:** [January 11, 2016, 9:32pm UTC](https://forums.unrealengine.com/t/fsocket-doesnt-stay-connected/343776 "2016-01-11T21:32:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Radiian](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Radiian](https://forums.unrealengine.com/u/Radiian)\
**Post date:** [January 11, 2016, 9:32pm UTC](https://forums.unrealengine.com/t/fsocket-doesnt-stay-connected/343776/1 "2016-01-11T21:32:56Z")

</div>

I’m playing around with FSocket trying to access a simple server I’ve thrown together in C# but I’m having issues. The server accepts the connection ok, but its immediately closed. I’m not sure if its a problem with my code in UE, or the C# server.

Here’s the server code in C#:

```
            socket.Accept();
            if (socket.Connected) textBox1.AppendText("Socket IS connected\r\n");
            textBox1.AppendText("Accepted a client!\r\n");
            byte[] received = new byte[256];
            int r;
            
            if (socket.Connected) { 
                r = socket.Receive(received);
                string response = Encoding.ASCII.GetString(received, 0, r);
                textBox1.AppendText("Client says: " + response + "\r\n");
            }
            else
            {
                textBox1.AppendText("Cant accept data on an unopen socket\r\n");
            }

```

Every time it results in the last else saying “Cant accept data on an unopen socket”

Here’s the code in UE (this is in the .cpp file, some of the variables are declared in the header):  
void AMyCharacter::ConnectToRemote()  
{

```
	FIPv4Address::Parse(address, ip);
	addr->SetIp(ip.GetValue());
	addr->SetPort(port);

	socketConnected = Socket->Connect(*addr);
}

bool AMyCharacter::sendFString(FString sendMe) {
	TCHAR *serialized = sendMe.GetCharArray().GetData();
	int32 size = FCString::Strlen(serialized);
	int32 sent = 0;

	bool success = Socket->Send((uint8*)TCHAR_TO_UTF8(serialized), size, sent);

	return success;
}

```

Everything seems to work in unreal engine, but I’m having a hard time working things out with such little documentation for this stuff. Anyone have any ideas?

---

<div class="post-metadata">

**Author:** ![Daekesh](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/daekesh/32/918652_2.png) [@Daekesh](https://forums.unrealengine.com/u/Daekesh)\
**Post date:** [January 11, 2016, 9:45pm UTC](https://forums.unrealengine.com/t/fsocket-doesnt-stay-connected/343776/2 "2016-01-11T21:45:29Z")

</div>

The bool returned from Connect isn’t a “yes, we’ve connected.” It’s a “yes, the connection attempt process started correctly.” You need to wait for the socket GetConnectionState() to go from SCS\_NotConnected to SCS\_Connected. And hopefully not SCS\_ConnectionError.

---

<div class="post-metadata">

**Author:** ![Radiian](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Radiian](https://forums.unrealengine.com/u/Radiian)\
**Post date:** [January 11, 2016, 9:50pm UTC](https://forums.unrealengine.com/t/fsocket-doesnt-stay-connected/343776/3 "2016-01-11T21:50:53Z")

</div>

Ah so thats a little different than I’m used to but helpful. How do I check what the error is if I get SCS\_ConnectionError? If I had better error info I’d be able to pinpoint the problem better.

---

<div class="post-metadata">

**Author:** ![Daekesh](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/daekesh/32/918652_2.png) [@Daekesh](https://forums.unrealengine.com/u/Daekesh)\
**Post date:** [January 11, 2016, 9:55pm UTC](https://forums.unrealengine.com/t/fsocket-doesnt-stay-connected/343776/4 "2016-01-11T21:55:48Z")

</div>

> **[ISocketSubsystem::GetLastErrorCode](https://docs.unrealengine.com/4.27/en-US/API/Runtime/Sockets/ISocketSubsystem/GetLastErrorCode/)**
>
> Returns the last error that has happened

---

<div class="post-metadata">

**Author:** ![Radiian](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Radiian](https://forums.unrealengine.com/u/Radiian)\
**Post date:** [January 11, 2016, 10:07pm UTC](https://forums.unrealengine.com/t/fsocket-doesnt-stay-connected/343776/5 "2016-01-11T22:07:02Z")

</div>

How is it used though? I’m not seeing documentation on the ESocketErrors type

---

<div class="post-metadata">

**Author:** ![Daekesh](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/daekesh/32/918652_2.png) [@Daekesh](https://forums.unrealengine.com/u/Daekesh)\
**Post date:** [January 11, 2016, 10:11pm UTC](https://forums.unrealengine.com/t/fsocket-doesnt-stay-connected/343776/6 "2016-01-11T22:11:18Z")

</div>

[https://github.com/EpicGames/UnrealEngine/blob/release/Engine/Source/Runtime/Sockets/Public/SocketTypes.h](https://github.com/EpicGames/UnrealEngine/blob/release/Engine/Source/Runtime/Sockets/Public/SocketTypes.h)

Your first port of call when you don’t know what a type is and it’s not in the docs? Check the unreal engine source. If you’re using VS, ctrl+shift+f.

---

<div class="post-metadata">

**Author:** ![Radiian](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Radiian](https://forums.unrealengine.com/u/Radiian)\
**Post date:** [January 12, 2016, 1:03am UTC](https://forums.unrealengine.com/t/fsocket-doesnt-stay-connected/343776/7 "2016-01-12T01:03:32Z")

</div>

Right, I forgot I had engine source lol, I’ll look into it

---

<div class="post-metadata">

**Author:** ![Radiian](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Radiian](https://forums.unrealengine.com/u/Radiian)\
**Post date:** [January 12, 2016, 7:15am UTC](https://forums.unrealengine.com/t/fsocket-doesnt-stay-connected/343776/8 "2016-01-12T07:15:55Z")

</div>

Ok honestly I have no idea what I’m doing. I thought I was getting somehwere, but my problem isn’t any better and I still can’t interpret the error code. How are you supposed to interpret ESocketErrors? I’m sure as hell not going to type out every single value into an if or switch statement, and so far I can’t convert it to a human readable string

---

<div class="post-metadata">

**Author:** ![Radiian](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Radiian](https://forums.unrealengine.com/u/Radiian)\
**Post date:** [January 15, 2016, 5:49am UTC](https://forums.unrealengine.com/t/fsocket-doesnt-stay-connected/343776/9 "2016-01-15T05:49:21Z")

</div>

I’m really stumped here. After checking the error code I’m not getting any error, but the socket will not stay open. What am I doing wrong?  
GetConnectionState() tells me that theres a connection error, but GetLastError() is telling me theres no error. WTF???

---

<div class="post-metadata">

**Author:** ![Radiian](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Radiian](https://forums.unrealengine.com/u/Radiian)\
**Post date:** [January 15, 2016, 7:35am UTC](https://forums.unrealengine.com/t/fsocket-doesnt-stay-connected/343776/10 "2016-01-15T07:35:56Z")

</div>

I’m an idiot. Apparently I had forgotten that socket.accept() returns a new socket and I can’t just communicate on that initial socket… Oops. Everything is fin now though and I feel dumb lol.
