Help with TCP Socket Connection... 4.15.3

Hi, I have a seemingly very straight forward problem that I’m trying to solve. I will preface the post with a little background…

I am outputting simulation data real time from a server and for now I simply want to pull this data into unreal and display it during runtime. Eventually, I will use the data to control actors (having them follow x-y cords etc) but I’m trying to learn the basics of networking for now. The server that I am attempting to connect too via TCP socket is written in python and takes a string that specifies which variable to call as input, then sends that variable in real time to the client, in this case, Unreal Engine 4.

I’ve read through every forum I could find with little luck. Rama’s write up was great but is outdated now and is useful if UE4 is acting as the “server side.” Other forum posts got me started but are incomplete in their explanations, or not detailed enough to for me to grasp them. The list of posts that I’ve read through:

Here is the actor class that I have so far. The code compiles but when the game is played, only the first UE_LOG message is displayed to the log. There is no indication of whether or not the socket connection has been verified.

CannonBallActor.h



// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Actor.h"
#include "Networking.h"
#include "Sockets.h"
#include "SocketSubsystem.h"
#include "CannonBallActor.generated.h"

UCLASS()
class TRICKCANNON_API ACannonBallActor : public AActor
{
	GENERATED_BODY()

	// Staticmesh component that will be visuals of cannon
	UPROPERTY(Category = Mesh, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = true))
	class UStaticMeshComponent* SphereMesh;
	
	UFUNCTION(BlueprintCallable, Category = "TCPConnection")
	void sendPacket();

public:	
	// Sets default values for this actor's properties
	ACannonBallActor();

protected:

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
};


CannonBallActor.cpp



// Fill out your copyright notice in the Description page of Project Settings.

#include "TrickCannon.h"
#include "Engine.h"
#include "EngineGlobals.h"
#include "CannonBallActor.h"

void ACannonBallActor::sendPacket()
{
	UE_LOG(LogTemp, Warning, TEXT("Connection manager!"));
	FSocket* Socket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT("default"), false);
	FString address = TEXT("11.12.6.110");
	int32 port = 10001;
	FIPv4Address ip;
	ip = FIPv4Address(11, 12, 6, 106);

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

	bool connected = Socket->Connect(*addr);

	if (connected)
	{
		UE_LOG(LogTemp, Warning, TEXT("Connected!"));
	}

	FString VarFetch = TEXT("Sample Text to be used later")

// Sets default values
ACannonBallActor::ACannonBallActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	
	struct FConstructorStatics
	{
		ConstructorHelpers::FObjectFinderOptional<UStaticMesh> PlaneSphereMesh;
		FConstructorStatics()
			: PlaneSphereMesh(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere'"))
		{
		}
	};
	static FConstructorStatics ConstructorStatics;

	// Create Static mesh for the component
	SphereMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PlaneMesh0"));
	SphereMesh->SetStaticMesh(ConstructorStatics.PlaneSphereMesh.Get());
	RootComponent = SphereMesh;
}

// Called when the game starts or when spawned
void ACannonBallActor::BeginPlay()
{
	Super::BeginPlay();

	UE_LOG(LogTemp, Warning, TEXT("game started!"));
}

// Called every frame
void ACannonBallActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}


Any help is greatly appreciated!

Are you missing part of your code? Did you ever get this working?

I’m trying to do the same thing, and I can’t see much on this online. From what I see you’re not keeping the Socket after ticks, it just gets deleted after BeginPlay is called. But I’m not sure, and you’re missing part of your code.

1 Like