简体   繁体   中英

Is it possible to set CanRead and CanWrite Properties of a NetworkStream objects in C#?

I want to create two NetworkStreams objects and i want that one should only support reading operation and the other one only writting operation. This is what i have tried to do but i got the error

   NetworkStream nStreamObj1 = new NetworkStream(clientSocket);
   NetworkStream nStreamObj2 = new NetworkStream(clientSocket);

   nStreamObj1.canRead = true;
   nStreamObj1.canWrite = false

   nStreamObj1.canRead = false;
   nStreamObj1.canWrite = true;

I got the error that CanRead and CanWrite are read-only properties! How can i set if possible those properties ?

Assuming the Socket can read and write, you can try the following:

NetworkStream nStreamObj1 = new NetworkStream(clientSocket, FileAccess.Read);
NetworkStream nStreamObj2 = new NetworkStream(clientSocket, FileAccess.Write);

Edit:

See Remarks section here

If you are unable to call the NetworkStream constructor (perhaps the stream is a result of a call to TcpClient.GetStream ), your only option would be to make a wrapper stream that overrides the CanRead / CanWrite properties. It might look something like this:

public class ReadOrWriteStream:Stream
{
    private readonly Stream srcStream;
    private readonly bool canRead;
    private readonly bool canWrite;
    private bool disposed;

    public ReadOrWriteStream(Stream srcStream, bool canRead, bool canWrite)
    {
        this.disposed = false;
        this.srcStream = srcStream;
        this.canRead = canRead;
        this.canWrite = canWrite;
    }

    public override void Flush()
    {
        srcStream.Flush();
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        return srcStream.Seek(offset,
                              origin);
    }

    public override void SetLength(long value)
    {
        if (!CanWrite)
            throw new NotSupportedException();
        srcStream.SetLength(value);
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        if(!CanRead)
            throw new NotSupportedException();
        return srcStream.Read(buffer,
                              offset,
                              count);
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        if(!CanWrite)                
            throw new NotSupportedException();
        srcStream.Write(buffer,
                               offset,
                               count);

    }

    public override bool CanRead
    {
        get
        {
            return srcStream.CanRead && canRead;
        }
    }

    public override bool CanSeek
    {
        get
        {
            return srcStream.CanSeek;
        }
    }

    public override bool CanWrite
    {
        get
        {
            return srcStream.CanWrite && canWrite;
        }
    }

    public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
    {
        if(!CanRead)
            throw new NotSupportedException();
        return srcStream.BeginRead(buffer,
                                   offset,
                                   count,
                                   callback,
                                   state);
    }

    public override int EndRead(IAsyncResult asyncResult)
    {
        return srcStream.EndRead(asyncResult);
    }

    public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
    {
        if (!CanWrite)
            throw new NotSupportedException();
        return srcStream.BeginWrite(buffer,
                                    offset,
                                    count,
                                    callback,
                                    state);
    }

    public override void EndWrite(IAsyncResult asyncResult)
    {
        srcStream.EndWrite(asyncResult);
    }

    public override long Length
    {
        get
        {
            return srcStream.Length;
        }
    }

    public override long Position
    {
        get
        {
            return srcStream.Position;
        }
        set
        {
            srcStream.Position = value;
        }
    }

    protected override void Dispose(bool disposing)
    {
        if(disposing && !disposed)
        {
            srcStream.Dispose();
            disposed = true;
        }
        base.Dispose(disposing);
    }

}

So:

var networkStream = myTcpClient.GetStream();
var readOnlyStream = new ReadOrWriteStream(networkStream, true, false);
var writeOnlyStream = new ReadOrWriteStream(networkStream, false, true);

You cannot set those values. They will depend on the state of the underlying socket that this network stream was created from.

Provide the appropriate FileAccess enumerated value in the constructor to set the readability and writability of the NetworkStream. The CanRead property is set when the NetworkStream is initialized.

So you cannot directly set those values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM