Hello there!
For the last few days I’ve been trying to create a way to communicate with my C#/WinForms app through UDP (initially I wanted to use TCP, thus incorrect class and method names, pls ignore that). I found these helpful-sounding FUdp/FTcp SocketBuilder, SocketSender and SocketReceiver classes in Networking.h. They seem so easy to implement, but I ended up being completely stuck here. The compiler error says
‘FUdpSocketReceiver’: no appropriate
default constructor available, see
declaration of ‘FUdpSocketReceiver’
and the same thing for the Sender one, pointing at my class’ constructor. Here’s the code:
#.h
UCLASS(Blueprintable, ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class CPPQUICKSTART_API UTCPHandler : public UActorComponent
{
GENERATED_BODY()
public:
UTCPHandler();
virtual void BeginPlay() override;
virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "TCP/UDP")
FString TCPIP;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "TCP/UDP")
int32 TCPPort;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "TCP/UDP")
bool useUDPRatherThanTCP;
UPROPERTY(BlueprintReadOnly, Category = "TCP/UDP")
bool isConnected;
UFUNCTION(BlueprintCallable, Category = "TCP/UDP")
bool TCPConnect();
//FInternetAddr TCPAddress;
UFUNCTION(BlueprintCallable, Category = "TCP/UDP")
bool TCPSend(FString TextToSend);
UFUNCTION(BlueprintCallable, Category = "TCP/UDP")
bool TCPStartListening();
FSocket* SocketHandle;
FUdpSocketReceiver SocketReceiver;
FUdpSocketSender SocketSender;
};
#.cpp
include “CPPQuickStart.h”
include “TCPHandler.h”
UTCPHandler::UTCPHandler()
{
bWantsBeginPlay = true;
PrimaryComponentTick.bCanEverTick = true;
SocketSender = FUdpSocketSender::FUdpSocketSender(SocketHandle, _T("default"));
SocketReceiver = FUdpSocketReceiver::FUdpSocketReceiver(SocketHandle, 0.1f, _T("default"));
isConnected = false;
TCPPort = 3200;
TCPIP = "127.0.0.1";
SocketHandle = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT("default"), true);
}
bool UTCPHandler::TCPConnect() {
FIPv4Address TCPIPConverted;
TCPIPConverted.Parse(TCPIP, TCPIPConverted);
TSharedRef<FInternetAddr> Address = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
Address->SetIp(TCPIPConverted.GetValue());
Address->SetPort(TCPPort);
FUdpSocketBuilder SocketBuilder("default");
SocketBuilder.BoundToAddress(TCPIPConverted);
SocketBuilder.BoundToPort(TCPPort);
SocketHandle = SocketBuilder.Build();
bool Connected = SocketHandle->Connect(*Address);
isConnected = Connected;
return isConnected;
}