Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

send byte[4096] via TcpClient will raise an ArgumentOutOfRangeException #11

Open
rexxiang opened this issue May 23, 2016 · 0 comments
Open

Comments

@rexxiang
Copy link

rexxiang commented May 23, 2016

The following code will reproduce the bug.

var endpoint = new IPEndPoint(IPAddress.Loopback, 6002);
using (var client = new TcpClient()) {
    client.ProtocolFactory = WebSocketClientFactory.Default;
    client.Open(endpoint);

    var dataLength = 4096;
    var bytes = new byte[dataLength];
    var resp = (string)client.ExecuteSync(bytes);

    WriteLine($"{DateTime.Now:HH:mm:ss fff}\t{resp}");
}

I think there must be something wrong in method: BufferStream.Write(byte[] buffer, int bufferOffset, int count).

int origCount = count;
CheckDisposed();
if(isReadOnly) throw new NotSupportedException();

int newEnd = checked((offset - origin) + count);
if (newEnd > length)
{
    Grow(newEnd);
    length = newEnd;
}

int chunkIndex = offset/NetContext.BufferSize, chunkOffet = offset%NetContext.BufferSize;
int thisPage = NetContext.BufferSize - chunkOffet;
var chunk = buffers[chunkIndex]; // will throw ArgumentOutOfRangeException if the buffer.Length == 4096
if (thisPage >= count)
{ 
    // can write to a single page
    Buffer.BlockCopy(buffer, bufferOffset, chunk, chunkOffet, count);
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant