Send Cyrillic text through Socket?

I have problem with sending Cyrillic text "УБА221" via C# socket. It turned to "#xxxxx221". Here is xxxxx are 5 strange alphabets I can't copy paste.

Server side(receiving side):

SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
int iRx = theSockId.thisSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
TextLabel.Text =TextLabel.Text+ szData;

Client side(sending):

subtext = "УБА221";
byte[] byData = System.Text.Encoding.UTF8.GetBytes(subtext);
socket.Send(byData);

Is there any idea why my code is not working?

Jon Skeet
people
quotationmark

Look at how you're encoding the text to send it:

byte[] byData = System.Text.Encoding.UTF32.GetBytes(subtext);

Now look at how you're decoding the binary data back into text:

System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();

You're using UTF-32 to encode, and UTF-8 to decode. That's not going to work. You need to use the same encoding on both sides.

Also note:

  • There's no guarantee that you'll have received the whole text in a single BeginReceive/EndReceive call
  • If you do just want to decode a single byte array, use Encoding.UTF8.GetString(bytes, 0, bytesToDecode) rather than going via Decoder.
  • If you want to handle the situation where you get part of a character in one read call and then part in another read, you will want to use Decoder, but you'll need to preserve the Decoder between read calls, as that stores the state of "not yet decoded data".

people

See more on this question at Stackoverflow