代码之家  ›  专栏  ›  技术社区  ›  obtein

内存流。写在客户端上有效,但在服务器上无效?

  •  1
  • obtein  · 技术社区  · 11 月前

    我对自己正在做的事情有一个非常基本的了解。所以我在学习一个关于C#聊天应用程序的教程,有两个类是完全相同的;虽然代码在ChatClient上运行得很好,但它甚至不能在ChatServer上编译。这怎么可能?

    编译器说=>CS7036:没有给出与所需参数对应的参数

    说明:

    • ChatClient允许用户输入用户名等详细信息,然后他们可以通过聊天框UI聊天。WPF项目。净值8.0
    • ChatServer正在监听想要连接的用户,并通知是否有连接,然后提供用户之间的通信。控制台项目。净值4.7.2
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ChatClient.Net.IO
    {
         class PacketBuilder
        {
            MemoryStream ms;
            public PacketBuilder()
            {
                ms = new MemoryStream();
                
            }
    
            public void WriteOpCode ( byte opcode )
            {
                ms.WriteByte( opcode );
            }
    
            public void WriteString ( string msg )
            {
                var msgLength = msg.Length;
                ms.Write(BitConverter.GetBytes(msgLength));
                ms.Write(Encoding.ASCII.GetBytes(msg)); 
            }
    
            public byte [] GetPacketBytes ()
            {
                return ms.ToArray();
            } 
    
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ChatServer.Net.IO
    {
        class PacketBuilder
        {
            MemoryStream ms;
            public PacketBuilder ()
            {
                ms = new MemoryStream();
    
            }
    
            public void WriteOpCode ( byte opcode )
            {
                ms.WriteByte( opcode );
            }
    
            public void WriteString ( string msg )
            {
                var msgLength = msg.Length;
                ms.Write( BitConverter.GetBytes( msgLength ) );
                ms.Write( Encoding.ASCII.GetBytes( msg ) );
            }
    
            public byte [] GetPacketBytes ()
            {
                return ms.ToArray();
            }
    
        }
    }
    

    ChatServer PacketBuilder class

    我试着看看是否有人遇到同样的问题,但找不到答案。提前感谢。

    1 回复  |  直到 11 月前
        1
  •  0
  •   dbc    11 月前

    你必须建立你的 ChatServer 代码与之前相比。NET版本比您的客户端:

    它成功的原因是,在 .NET Core 2.1及更高版本 ,添加了新的过载 MemoryStream.Write() :

    MemoryStream.Write(ReadOnlySpan<Byte>) 
    

    使用从缓冲区读取的数据将字节块写入当前流。

    自从 byte [] implicitly convertible ReadOnlySpan<Byte> ,代码编译。

    但在 .NET Framework ,唯一可用的过载是:

    MemoryStream.Write(byte[] buffer, int offset, int count);
    

    如您所见 offset count 论点是 非可选 ,因此您必须将它们传入才能在中成功编译。NET Framework。你甚至可以这样创建一个扩展方法:

    public static class MemoryStreamExtensions
    {
        public static void Write(this MemoryStream ms, byte[] buffer)
        {
            if (ms == null)
                throw new ArgumentNullException("ms");
            ms.Write(buffer, 0, buffer.Length);
        }
    }
    

    现在,您的代码在两个版本中都将以相同的方式编译,请参阅 https://dotnetfiddle.net/k2RuFi https://dotnetfiddle.net/ZOEuNF .