I want my UE4 application (on server) to send data via TCP to another computer (client). For now, let us have the client and server both be on localhost. IP: 127.0.0.1 Port 15000
I do NOT want to use an actor or pawn or something else - I want this to be in a separate class that I can use in a way like: myDataSender.send(data);
Now, I have seen Ramas tutorial and tried many others. I can get the socket to connect on the server and the client appears to connect. However, no data appears to be sent - although, I am not exactly sure how to test that other than by having a breakpoint on my client to see when data arrives.
Here is what I am doing in my UE4 server application:
.h
class ImageDataSender
{
public:
ImageDataSender();
~ImageDataSender();
FSocket* TCPSocket;
bool Connect(FIPv4Address ipAddress, int port);
bool Send(const uint8 * data, int32 dataSize, int32 bytesSent);
bool IsConnected();
};
.cpp
bool ImageDataSender::Connect(FIPv4Address ipAddress, int port) {
TSharedRef<FInternetAddr> address = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
address->SetIp(ipAddress.GetValue());
address->SetPort(port);
TCPSocket = FTcpSocketBuilder("ImageSocket")
.AsNonBlocking()
.AsReusable()
.BoundToAddress(ipAddress.GetValue())
//.Listening(8)//max number of connections allowed before refusing
.BoundToPort(port);
bool didConnect;
if (TCPSocket) {
UE_LOG(LogTemp, Warning, TEXT("Socket Created! Connecting to server..."));
didConnect = TCPSocket->Connect(*address);
}
else {
UE_LOG(LogTemp, Warning, TEXT("Socket not created..."));
return(false);
}
if (didConnect) {
UE_LOG(LogTemp, Warning, TEXT("Socket Connected!"));
return(true);
}
else {
UE_LOG(LogTemp, Warning, TEXT("Failed to connect!!"));
return(false);
}
}
bool ImageDataSender::IsConnected() {
if (TCPSocket == nullptr) {
return(false);
}
ESocketConnectionState connectionState = TCPSocket->GetConnectionState();
if (connectionState == ESocketConnectionState::SCS_Connected) {
return(true);
}
else {
return(false);
}
}
bool ImageDataSender::Send(const uint8 * data, int32 dataSize, int32 bytesSent) {
if (IsConnected()) {
TCPSocket->Send(data, dataSize, bytesSent);
return(true);
}
return(false);
}
If I uncomment the line //.Listening(8)
, the didConnect == false
. I feel that I should be using .Listening
so that the server listens for incoming connection requests, but am not sure what I am doing wrong.
If someone could give me a sample of how to set this up, I’d really appreciate it.
Also, for what it is worth, my client is in c#.