Hello guys, I am playing around with the UDP/TCP Sockets of Unreal Engine 4 and got some problems while using the namespaces and functions like the documentation told me how to implement these.
Quiet simple here is just my test Header file:
#include “Kismet/BlueprintFunctionLibrary.h”
#include “Runtime/Sockets/Public/Sockets.h”
#include “Runtime/Networking/Public/Interfaces/IPv4/IPv4Address.h”
#include “Runtime/Core/Public/Templates/SharedPointer.h”
#include “Runtime/Sockets/Public/SocketSubsystem.h”
#include “Runtime/Networking/Public/Networking.h”
#include “Runtime/Sockets/Private/BSDSockets/SocketsBSD.h”
//#include “Runtime/Sockets/Private/BSDSockets/SocketSubsystemBSD.h”
#include “TCPConnection.generated.h”
/**
*
*/
UCLASS(BlueprintType, Blueprintable)
class UTCPConnection : public UBlueprintFunctionLibrary
{
GENERATED_UCLASS_BODY()
UFUNCTION(BlueprintCallable, Category = "TCPConnection")
void sendPacket();
};
And my CPP class:
// Fill out your copyright notice in the Description page of Project Settings.
#include "TCPTest.h"
#include "TCPConnection.h"
UTCPConnection::UTCPConnection(const class FObjectInitializer& PCIP)
: Super(PCIP)
{
}
void UTCPConnection::sendPacket(){
//FSocket* Socket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT("default"), false);
FSocketBSD* Socket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT("default"), false);
FString address = TEXT("127.0.0.1");
int32 port = 12345;
FIPv4Address ip;
//FIPv4Address::Parse(address, ip);
ip = FIPv4Address(127, 0, 0, 1);
//TSharedRef<UTCPConnection> addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
//auto addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
TSharedRef<FInternetAddr> addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
addr->SetIp(ip.GetValue());
addr->SetPort(port);
bool connected = Socket->Connect(*addr);
FString serialized = TEXT("teststring|999");
TCHAR *serializedChar = serialized.GetCharArray().GetData();
int32 size = FCString::Strlen(serializedChar);
int32 sent = 0;
bool successful = Socket->Send((uint8*)TCHAR_TO_UTF8(serializedChar), size, sent);
}
So some functions inside the documentation are a bit outdated and not reliable or replicatable for me. For example
I used the “Fsocket” before and noticed that it is not supported from Windows anymore so I have to use FSocketBSD instead.
I am also not able to use the ISocketSubsystem like all the old tutorials or codes found on the forums or answerhub.
VS is underlining “ISocketSubsystem” inside these following lines:
FSocketBSD* Socket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT("default"), false);
and
TSharedRef<FInternetAddr> addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
I also noticed that there is an undocumented Class called “FSocketSubsystemBSD” which could fix this maybe but I generally don’t have a plan how to use this class.
Maybe you’ll have some ideas for me
Greetings and ty guys