是否可以在C#中设置NetworkStream对象的CanRead和CanWrite属性?

时间:2011-12-06 08:42:53

标签: c# stream

我想创建两个 NetworkStreams 对象,我希望它只支持读取操作,而另一个只支持写入操作。这是我试图做但我得到错误

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

   nStreamObj1.canRead = true;
   nStreamObj1.canWrite = false

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

我收到CanRead和CanWrite是只读属性的错误!如何设置这些属性?

4 个答案:

答案 0 :(得分:4)

假设Socket可以读写,您可以尝试以下方法:

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

编辑:

请参阅备注部分here

答案 1 :(得分:2)

如果您无法调用NetworkStream构造函数(可能是调用TcpClient.GetStream的结果),您唯一的选择是创建一个覆盖{{1}的包装器流} / CanRead属性。它可能看起来像这样:

CanWrite

所以:

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

}

答案 2 :(得分:0)

您无法设置这些值。它们将取决于创建此网络流的底层套接字的状态。

答案 3 :(得分:0)

在构造函数中提供相应的FileAccess枚举值,以设置NetworkStream的可读性和可写性。初始化NetworkStream时设置CanRead属性。

所以你不能直接设置这些值。