FSocket doesn't stay connected

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?

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.

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.

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

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.

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

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

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???

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.