I have gathered as much data as I could find in the documentation as well as around the web on UE4’s socket networking ability. I have successfully connected to a server and sent data. The server echos the sent data back to the client. However, FSocket->Recv(blah, blah, blah) isn’t reading any data back from the server. I have another client written in C# that does receive the data echo.
void ANetwork_Test::TestTransmit(FString outData){
// Stores the final server IP.
FIPv4Address serverIP;
// The ip address of the serverList Server.
FString serverIpAddress = "192.168.1.11";
// Port for connection to the serverList Server.
int32 serverListPort = 24575;
// Turn the string Ip into a usable ip and store it in serverIP.
FIPv4Address::Parse(serverIpAddress, serverIP);
// Create a network socket to connect to the serverList Server.
sendSock = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT("sendSock"), false);
// Get a socketable internet address for use in the socket.
TSharedRef < FInternetAddr > intAddr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
// Set the IP and PORT of the new socketable internet address.
intAddr->SetIp(serverIP.GetValue());
intAddr->SetPort(serverListPort);
// Attempt a connection and store result in the connectionAttempt bool.
bool connectionAttempt = sendSock->Connect(*intAddr);
if (connectionAttempt){
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Connect attempt SUCCESS!"));
SendData(sendSock,outData);
}
else{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Connect attempt failed."));
return;
}
}
This is the SendData code:
void ANetwork_Test::SendData(FSocket* client, FString data){
TCHAR *sendData = data.GetCharArray().GetData();
int32 size = FCString::Strlen(sendData);
int32 sent = 0;
uint8 *inData = 0;
FString inDataString = "NO DATA";
int32 byteCount = 0;
uint32 dataSize = 0;
client->Send((uint8*)TCHAR_TO_UTF8(sendData), size, sent);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Data away!"));
client->HasPendingData(dataSize);
if (dataSize != 0){
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Got something."));
client->Recv(inData, dataSize, byteCount);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::FromInt(byteCount));
}
}
I haven’t been able to get the data back from the server in UE4 but it works in the C# socket client I have.
Any thoughts?
Thanks,
-StewVanB