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:
- A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums
- https://wiki.unrealengine.com/TCP_Socket_Listener,Receive_Binary_Data_From_an_IP/Port_Into_UE4,(Full_Code_Sample)
- Shimstar: Third Party server with UE4 : part 3 : C++ : connect and read socket
- I need to connect to server via TCP - Multiplayer & Networking - Unreal Engine Forums
- FSocket::connect() always return false in android when NonBlocking is used. - Feedback for Unreal Engine team - Unreal Engine Forums
- http://www.osnapgames.com/2014/05/24/connecting-to-a-third-party-socket-server-in-unreal-engine-4/
- What Includes are needed to set up Networking and Sockets? - Multiplayer & Networking - Unreal Engine Forums
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!