FSocket->Recv no data response

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

I would be interested in figuring this out as well. :expressionless: Having this same issue.

Hi, did you find an answer? im able to recieve data from my server, but it prints it as 0.

Hello, StewVanB

I am sorry to hear about your problem.

Please note that the common practice in this case is to put your send and response-handling logic in separate functions. This way, you can add a function like this:

void ReceiveMessage()
{
    // assuming that variables are initialized correctly
    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));
    }

Now you can call it in timer:

 FTimerHandle MyTimerHandle;
 GetWorldTimerManager().SetTimerMyTimerHandle, this, &AMyActor::ReceiveMessage, 0.01, true);

Thus, you should get appropriate messages.

Hope this helped!

Have a great day!

Hello, I have tested your code in my project, and I am having the same problem as Stew.

Using the send method, my server crashes when it receives data.

Sending data from the server to the client, for example a string of test, prints out as -1.

Have you ever encountered this problem?

Same problem, again.
Try the method, Not solved

I was having the same problem.

The problem is that the buffer is null. with the changes the bytecount and the dataSize should be the same size.

void HandleRecieve()
{
uint32 dataSize = 0;
client->HasPendingData(dataSize);

if (dataSize != 0) 
{
	uint8 *inData = new uint8[dataSize];
	int32 byteCount = 0;

	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Recieved data from server."));
	socket->Recv(inData, dataSize, byteCount);
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::FromInt(byteCount));
}

}