Send a value by socket and read the value by reader.ReadInt32() changes value

一笑奈何 提交于 2019-11-29 12:56:24

As far as I can tell from your code, you are sending data as a string in binary format, this will yield bytes for the characters 1,2.

When you read the data back you try to get Int32 values.

There are two options here:

Read and write data as a string.

 Client code:

 binaryWriter.Write("1,2");

 Server code:

 string text = binaryReader.ReadString(); // "1,2"

OR Read and write data as integers.

Client code:

binaryWriter.Write(10);
binaryWriter.Write(20);

Server code:

int value1 = binaryReader.ReadInt32(); //10
int value2 = binaryReader.ReadInt32(); //20

and my values is like this :841757955

Always worth sticking that number in the Windows calculator and convert that to hex. You get 0x322C3503.

Which looks a lot like ASCII, a string with 3 characters that encodes "5,2". In other words, your real code doesn't resemble your snippet at all, you don't actually use the BinaryWriter.Write(Int32) overload, you used BinaryWriter.Write(String).

Sure, that can't work, you can't write a string and expect it to be readable as raw integers. Fix your code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!