Download

Can't receive messages from a C# server

Hey, I’m creating a dedicated server on c#, and when a client connects it sends a message.

The message is received, but only 0 is printed on the screen.

The Server-----



               byte] data = new byte[1024];
                string name = "SERVER YUOU";
                data = Encoding.ASCII.GetBytes(name);
                ns.Write(data, 0, data.Length);


The Client in C++



	FString serialized = TEXT("SentFromClient|1");
	TCHAR *serializedChar = serialized.GetCharArray().GetData();
	int32 size = FCString::Strlen(serializedChar);
	int32 sent = 0;
	bool successful = Socket->Send((uint8*)TCHAR_TO_UTF8(serializedChar), size, sent);
	//if (successful)
		//GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("This is an on screen messag lkoile!"));

		//GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("This is an on screen messag lkoile!"));
	uint8 *inData = 0;
	int32 byteCount = 0;
	uint32 dataSize = 0;

	int32 Read = 0;
	bool hasData = Socket->HasPendingData(dataSize);
	if (hasData)
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("This is an on rretrte messag lkoile!"));

		bool yes = Socket->Recv(inData, dataSize, byteCount);
		if (yes)


When the client connects, i get a yellow “0” shown on the screen. The server did not send this.

Where did i go wrong?

That c++ code doesn’t appear to be valid code.

FString is a wide string, so try Encoding.Unicode in C#

Try using Encoding.UTF8 in C#.

FString is a wide string (2 bytes per Char) and u do a TCHAR_TO_UTF8

Hello, can you please explain why it isn’t valid. I didn’t get any compiling errors with Ue4

Hello, I have tried this; my server sends hello, and the client Ue4 prints it as 0. The message is received, but not still printed as 0.

Maybe it is just the formatting from the forum, but this:



	if (hasData)
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("This is an on rretrte messag lkoile!"));

		bool yes = Socket->Recv(inData, dataSize, byteCount);
		if (yes)


Is technically valid, but misleading and not doing what one would expect in some cases. It also just stops.

Hey John thanks for the reply, and apologies for getting back to you.

The problem is that my client does receive a message from the server, and when it prints out the message it displays the number “-1” instead of the string value.

Check this simple…c# socket program