C# BinaryWriter .Write()方法发送string字符串存在问题

直接使用 .Write(message)发送字符串message会导致消息头部多出一个随机的字节

private BinaryWriter bw;
...
bw = new BinaryWriter(networkStream);
...
bw.Write(message);

受限于知识水平,我无法找出问题的根本原因,只能给出如下的解决方式:

private BinaryWriter bw;
...
bw = new BinaryWriter(networkStream);
...
bw.Write(Encoding.Default.GetBytes(message), 0, message.Length);

 

你可能感兴趣的:(C#)