0

Client: [1]: https://i.stack.imgur.com/YT0Pn.png

Server: [2]: https://i.stack.imgur.com/EWlEQ.png

I'm communicating between sockets, but why is the data coming like this?

Client Code:

            var buffer2 = new byte[2048];
            int received2 = ClientSocket.Receive(buffer2, SocketFlags.None);
            if (received2 == 0) return;
            var data2 = new byte[received2];
            Array.Copy(buffer2, data2, received2);
            string text2 = Encoding.ASCII.GetString(data2);
            games_image.Add(text2);
            Thread.Sleep(500);

Server Code:

                        Console.WriteLine(games_image[i]);

                        byte[] data = Encoding.ASCII.GetBytes(games_image[i].ToString());
                        current.Send(data);
                        Thread.Sleep(500);

  • 2
    I hope you had chance to read [Encoding.ASCII](https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.ascii?view=net-6.0) (in particular "It uses replacement fallback to replace each string that it cannot encode and each byte that it cannot decode with a question mark ("?") character.")? It would be good idea to [edit] the question to clarify that, as well as to justify why you decided to use that encoding. Also replace pictures with text (as it looks like images are just showing text anyway) – Alexei Levenkov Apr 20 '22 at 02:43
  • @Wyck the images show how Encoding.ASCII supposed to work (converting what looks like Turkish-I into question mark). Not really sure what OP wanted to demonstrate with those or why they couldn't use text to show it... – Alexei Levenkov Apr 20 '22 at 02:45
  • Welcome to SO! Try to avoid posting with titles like _"What is the reason for this?"_ as it's not very informative to readers. You want to grab readers attention. Remember posts in SO are around for a long time giving you the chance to gain additional reputation from future readers and not just those from today. For this reason, a good post should be _search engine friendly._ Good luck! –  Apr 20 '22 at 02:50
  • You are also not framing your messages. You are using a stream protocol which only guarantees that bytes are delivered in order, but they may be repackaged into different chunks. For example, you may send `COMPUTER` but then you may receive it in 3 different Receive calls `COM`, then `PU`, then `TER`. This can be particularly hazardous with multi-byte UTF-8 character encodings because the socket layer doesn't know not to split them up, and it isn't obligated to. It's up to you to turn the socket stream protocol into a message-based protocol with delimiters or by transmitting the buffer size. – Wyck Apr 20 '22 at 02:56
  • ASCII is so 1963. I wonder if there are newer standards to use? – Enigmativity Apr 20 '22 at 03:25

1 Answers1

0

İ is not a character in ASCII. It has no ASCII code. So when you ask for the ASCII codes of your string (Encoding.ASCII.GetBytes) .NET is nice enough to give you a question mark ASCII code, instead of just crashing.

If you want to be able to send characters that don't have ASCII codes (but do have Unicode codes), you should try Encoding.UTF8 instead.


Note: It looks like you are expecting one Send call to match up with one Receive call. Assuming you are using TCP, it doesn't work that way in TCP, and I expect you will run into this extremely common beginner problem later. See Python TCP Socket Is Merging Data? or How to fix recv merging multiple TCP segments into a single one? or C# Socket is every one Receive corresponds to one Send?

user253751
  • 57,427
  • 7
  • 48
  • 90