Search code examples
c#tcpcompiler-errorsmemorystream

MemoryStream.Write works on Client but not on Server?


I have a very basic understanding about what I am doing. So I was following a tutorial about a chat app on C# and there are 2 classes which are comletely same however; while the code works completely fine on ChatClient, it doesnt even compile on ChatServer. How is this possible?

Compiler says => CS7036: There is no argument given that corresponds to the required parameter

Explanation :

  • ChatClient is for users to enter their details like UserName etc. and then they can chat through a chatbox UI. WPF project .Net 8.0
  • ChatServer is listening users who want to connect and informs if any connected then provides communication between users. Console Project .Net 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

I have tried to see if anyone got the same problem but couldn't find the answer. Thanks in advance.


Solution

  • You must be building your ChatServer code against an earlier .NET version than your client:

    The reason it succeeds is that, in .NET Core 2.1 and later, a new overload was added to MemoryStream.Write():

    MemoryStream.Write(ReadOnlySpan<Byte>) 
    

    Writes a block of bytes to the current stream using data read from a buffer.

    Since byte [] is implicitly convertible to ReadOnlySpan<Byte>, the code compiles.

    But in .NET Framework, the only overload available is:

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

    As you can see, the offset and count arguments are not optional, so you must pass them in to compile successfully in .NET Framework. You could even make an extension method like so:

    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);
        }
    }
    

    And now your code will compile identically in both versions, see https://dotnetfiddle.net/k2RuFi and https://dotnetfiddle.net/ZOEuNF.