TCP/UDP problem with ISocketSubsystem

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 :slight_smile:

Greetings and ty guys

I forgot: This comes up in 4.6.1 & 4.7.0 release
I also had to include all these :

#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"

because:
#include “Networking.h”
#include “Sockets.h”

was not working, also after including it here:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "Sockets", "Networking" });

I didn’t have a problem using FSocket on windows in 4.7.1

.h - I only included these 3

#include "Networking.h"
#include "Sockets.h"
#include "SocketSubsystem.h"

.cpp - my connect function. My implimentation is still very much a prototype, hence the UE_LOG and placeholder SOCKETNAME

void ConnectionManager::Connect(FIPv4Address ip, int32 port)
{
	UE_LOG(LogTemp, Warning, TEXT("ConnectionManager::Init"));

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

	TCPSocket = FTcpSocketBuilder("SOCKETNAME").AsNonBlocking().AsReusable().Build();


	UE_LOG(LogTemp, Warning, TEXT("Socket created! Connecting to server..."));
	bool didConnect = TCPSocket->Connect(*addr);
	if (didConnect)
	{
		UE_LOG(LogTemp, Warning, TEXT("Connection state: true"));

		//Create GameClient
		GameClientInstance = new GameClient();

		//Check for updates
		TickDelegateHandle = FTicker::GetCoreTicker().AddTicker(FTickerDelegate::CreateRaw(this, &ConnectionManager::Tick), 0.1f);
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("Connection state: false"));
	}
}