Why does UE4 always miss the first packet?

Hi,

I press a button, and it sends a packet, containing a string data “Hello” to my server.

My C# server receives it, and then sends it back to the client (UE4).

My UE4 doesn’t get the packet.

  1.     TArray<uint8> ReceivedData;
    
  2.     uint32 Size;
    
  3.     while (mySocket->HasPendingData(Size))
    
  4.     {
    
  5.         ReceivedData.SetNumUninitialized(FMath::Min(Size, 65507u));
    
  6.         int32 Read = 0;
    
  7.         mySocket->Recv(ReceivedData.GetData(), ReceivedData.Num(), Read);
    
  8.     }
    
  9.     if (ReceivedData.Num() <= 0)
    
  10.     {
    
  11.         FString temp = TEXT("False");
    
  12.         return temp;
    
  13.     }
    

I press the button the second time,

Server gets it, and now client gets it. And so on.

Why does the client miss the first one?

It’s bad, because if my players type in the correct password and username the first time, the server will say that’s right, but the client will not receive, until the player presses the button the second time.

Thank you.

EDIT--------------------------------------------

So I made a method on my game instance class, that retrieves the players name from the database. when I call this function, it sends a request to the database.

When my server gets the packet, it instantly sends a packet back to the client. However


bool successful = mySocket->Send((uint8*)TCHAR_TO_UTF8(serializedChar), size, sent);
	if (successful)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "Sent");

	}


	TArray<uint8> ReceivedData;

	uint32 Size;
	while (mySocket->HasPendingData(Size))
	{
		ReceivedData.SetNumUninitialized(FMath::Min(Size, 65507u));

		int32 Read = 0;
		mySocket->Recv(ReceivedData.GetData(), ReceivedData.Num(), Read);

		//GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Data Read! %d"), ReceivedData.Num()));
	}
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	if (ReceivedData.Num() <= 0)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "nothing");

		FString temp = TEXT("Error|Could not recieve from server.");
		return temp;

	}

Even though my server did send something, the client socket did not pick ut.

I got the “Nothing” and Error debug message.

Why is my socket not listening for incoming messages, when I clearly tell it to? Or is the problem it must always be listening for messages, for example, like in a thread? Since this is in the game instance class, the game instance does not have a tick method, so I can’t do that.

Any suggestions pelase?

Have u solved this issue ? I got a similar one. Upon connected with server, I immediately send buffer to server, and always return -1 of byte-sent.