How to I get my Socket to start listening?

Hi, from looking at Ramas tutorial Rama<3 I’ve attempted to implement a listen server (that will do light work in the b.ground).

UServer::UServer()
{
	ReceiveBufferSize = 2 * 1024 * 1024;
}

void UServer::StartListening(UWorld* World)
{
	const FString SocketName = FString(TEXT("Socket"));
	const FIPv4Endpoint EndPoint = FIPv4Endpoint(FIPv4Address::InternalLoopback, 12345);

	Socket = FTcpSocketBuilder(SocketName).AsReusable().Listening(8).BoundToEndpoint(EndPoint);
	int32 NewSize = 0;
	Socket->SetReceiveBufferSize(ReceiveBufferSize, NewSize);
	//GetOuter()->GetWorld()->GetTimerManager().SetTimer(listenHandle,this, &UServer::Listerner, 0.01, true);
	if (World)
	World->GetTimerManager().SetTimer(listenHandle,this, &UServer::Listerner, 2, true);


}

void UServer::Listerner()
{
	if (!Socket) return;
	//~~~~~~~~~~~~~

	//Remote address
	TSharedRef<FInternetAddr> RemoteAddress = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
	bool Pending;

	// handle incoming connections
	if (Socket->HasPendingConnection(Pending) && Pending)
	{
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		//Already have a Connection? destroy previous
		if (ConnectionSocket)
		{
			ConnectionSocket->Close();
			ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(ConnectionSocket);
		}
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

		//New Connection receive!
		ConnectionSocket = Socket->Accept(*RemoteAddress, TEXT("RamaTCP Received Socket Connection"));
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "debug msg");

		if (ConnectionSocket != NULL)
		{
			//Global cache of current Remote Address
			RemoteAddressForConnection = FIPv4Endpoint(RemoteAddress);

			//UE_LOG "Accepted Connection! WOOOHOOOO!!!";

			//can thread this too

		}
	}
}

Before executing the timer I tired using the Listen method which did nothing.

In my test C# client program I attempt to connect, but no connection is made.

            TcpClient c = new TcpClient();
            c.Connect(IPAddress.Loopback, 12345);
            Console.ReadLine();

Am I missing something, surely Socket->Accept() is a blocking method, that stops the flow of code, untill a client connects, right?

I made sure to open ports 12345 in my fire wall as well.

Does anyone know why my dummby client cannot connect to the server?

I couldn’t find the answer to get my TCP listener to work in the engine.

It would work in the C# server, but not the UE4 server.

The project required me to send some data to a server that acts as a high scores board. For this, it would be quite silly to have the client connect, so instead I made the “server” a UDP listener that listens for UDP packets.

This enabled my UE4 client to send information to the UE4 UDP Server.

Props to Ramas UDP tutorial.

1 Like

Were you able to do this? If yes, could you explain how?

The OP code is missing anything that would actually make it work… I can’t seem to find a replacement for the link exactly, since it goes to the old wiki which is long gone… but…

this looks like someone mirrored it from the old wiki, before it went away. Unfortunately, the formatting is completely destroyed, but maybe you can recover it

It looks like that at least has the code to listen to a TCP socket and accept connections on it. (those are some keywords you might need if you want to do more searching on it)

No, this thread is for sending packets from a python server controlled by you to an in-game actor or actor → python server → another actor , this does not use dedicated server, I found a similar thread like this for UDP if I were to use this I would have to recreate the whole game, all the RPC calls between actors and variables that were replicated would have to be written and changed in another way.

This thread is about making a socket server in Unreal, as far as I can tell. Nothing about game’s dedicated server, or python.

You do need to Listen to a socket, and Accept connections on that port, to be able to talk to clients.