'What is the reason for this?
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);
Solution 1:[1]
? 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?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user253751 |
