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

前端 未结 2 1507
太阳男子
太阳男子 2020-12-20 10:35

I am trying to send a value by socket .So i have two parts in my project Client and server .

The client sends a value to serve

2条回答
  •  再見小時候
    2020-12-20 10:47

    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
    

提交回复
热议问题