You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Exception in websock receive in .NET Core 2.1. Works in .NET Framework 4.6.2 (same code). This happens when receiving large binary data. I tested receiving binary data that is approx. 50K and 100K. In all cases the websocket receive throws the exception.
Visual Studio 15.8.0
My log from my process shows exception:
Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
privatevoidThreadNetworkLoop(objectnpObject){byte[]buffer=newbyte[8192];boolloop=true;NetProtocolnpSource=((State)npObject).npSource;NetProtocolnpDestination=((State)npObject).npDestination;Console.WriteLine("Starting Thread "+Thread.CurrentThread.ManagedThreadId);try{intbytesRead=0;while(loop){bytesRead=npSource.Receive(buffer,0,buffer.Length,Timeout.Infinite,false);Console.WriteLine("Thread:"+Thread.CurrentThread.ManagedThreadId+" Received "+bytesRead);if(bytesRead>0){npDestination.Send(buffer,bytesRead);}else{Console.WriteLine("Thread:"+Thread.CurrentThread.ManagedThreadId+" 0 bytes received -- closing");npSource.Close();npDestination.Close();loop=false;}}}catch(Exceptione){Console.WriteLine("Exception :"+e.Message);npSource.Close();npDestination.Close();}}
The above npSource.Receive calls the following routine (below)
My Websock receive code looks like this:
publicintReceive(WebSocketws,byte[]buffer,intoffset,intexpectedSize,inttimeout=Timeout.Infinite,boolwaitForExpectedSize=true){boolstop=false;intcount=0;if(expectedSize==0)return0;try{while(!stop&&(count<expectedSize)){TraceLog.WriteLineThr($" offset: {offset}, count: {count}, expected size: {expectedSize}");// we keep moving the segment over (offset) until we read everythingArraySegment<byte>tempBuffer=newArraySegment<byte>(buffer,offset,expectedSize-count);// might have to do this in a loop until we received a End Of Message flagTask<WebSocketReceiveResult>receiveResult=ws.ReceiveAsync(tempBuffer,CancellationToken.None);receiveResult.Wait(timeout);// do something, end of the message. Note: Not really needed as we know how many bytes to read//if (receiveResult.Result.EndOfMessage)// break;if(receiveResult.IsFaulted||receiveResult.IsCanceled){TraceLog.WriteLineThr("WebSocket canceled/faulted received.");return0;}elseif(receiveResult.Result.MessageType==WebSocketMessageType.Close){TraceLog.WriteLineThr("Websocket close received.");return0;}elseif(receiveResult.Result.Count<=0){TraceLog.WriteLineThr("received 0 bytes (Should we close?)");return0;}elseif((receiveResult.Result.Count>0)&&(receiveResult.Result.Count<expectedSize)){TraceLog.WriteLineThr("received less then expected. Read:"+receiveResult.Result.Count+" Expected:"+expectedSize+" waitForExpectedSize:"+waitForExpectedSize);if(!waitForExpectedSize)stop=true;}elseif((receiveResult.Result.Count>(expectedSize-count))){// This one can't happen as we pass in a ArraySegment with the correct sizeTraceLog.WriteLineThr("Error read more then expected, buffer overflow. Received:"+receiveResult.Result.Count);return0;}//Array.Copy(tempBuffer.Array, 0, buffer, count, receiveResult.Result.Count);offset+=receiveResult.Result.Count;count+=receiveResult.Result.Count;receivedBytes+=(ulong)receiveResult.Result.Count;//TraceLog.WriteLineThr("Websocket: read: " + receiveResult.Result.Count + ", count: " + count + ", expected: " + expectedSize);}TraceLog.WriteLineThr("returning count: "+count);returncount;}catch(AggregateExceptionae){foreach(Exceptionexinae.InnerExceptions)TraceLog.WriteLineThr(ex.Message);return0;}catch(Exceptione){TraceLog.WriteLineThr(e.Message);return-1;}}
The text was updated successfully, but these errors were encountered:
DigitalNut
changed the title
Net Core exception in websock receive (works in .Net Framework 4.6.2)
Net Core exception in websock receive (works in .Net Framework 4.6.2) when sending large data
Aug 21, 2018
Do you have minimal repro we can try locally? (not just code snippets, but something that compiles)
Please try to remove all unnecessary code from it ...
My mistake. It's working on .NET core 2.1 latest version that ships with Visual Studio 15.8.1. I had updated to 15.8.0 but forget to check if the update .NET Core 2.1 runtime fixed the problem. It working great with .NET Core 2.1 and the .NET framework. I tested with small & large binary data (1.4MB) and it worked great. Sorry for the inconvenience!
Exception in websock receive in .NET Core 2.1. Works in .NET Framework 4.6.2 (same code). This happens when receiving large binary data. I tested receiving binary data that is approx. 50K and 100K. In all cases the websocket receive throws the exception.
Visual Studio 15.8.0
My log from my process shows exception:
Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
Attached is my log from process.
Net2.1ExceptionWebsocket.txt
I'm not sure if this is the same issue reported on StackOverflow
https://stackoverflow.com/questions/47233356/dotnet-core-2-websocket-receiveasync-throws-exception-with-small-buffer
This is my outer thread loop looks like this:
The above npSource.Receive calls the following routine (below)
My Websock receive code looks like this:
The text was updated successfully, but these errors were encountered: