Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# BinaryReader.ReadChar throws "System.ArgumentException: The output char buffer is too small" when reading NetworkStream

When reading C# NetworkStream (from stream-type TCP socket), BinaryReader.ReadChar occasionally throws exceptions System.ArgumentException: The output char buffer is too small to contain the decoded characters, encoding 'Unicode (UTF-8)'

All buffers have their default sizes (none of them are manually set) and setting bigger buffer sizes does not affect the issue.

And what is completely discouraging:

  • The exception does not occur when using breakpoint and walking step-by-step through the line with ReadChar call

  • The exception does not occur if ReadChar is preceded by Thread.Sleep(1000) (but can still occur with smaller timeouts)

  • The exception does not occur when using BinaryReader on FileStream, where all precise bytes of TCP-server's answer are stored.

So, what can be the time-related issue with buffering single characters from stream a socket?

like image 906
user2732454 Avatar asked Aug 30 '13 10:08

user2732454


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Why do we write C?

We write C for Carbon Because in some element the symbol of the element is taken form its first words and Co for Cobalt beacause in some elements the symbol of the element is taken from its first second letters, so that the we don't get confuse.


1 Answers

I had this issue too. And here are some facts about it:

  1. System.ArgumentException: The output char buffer is too small to contain the decoded characters, encoding 'Unicode (UTF-8)' is known to be related to UTF-8 encoding problem (invalid character code) rather than to buffering problem - Detials here

  2. NetworkStream (Read and other methods) is known to return only the amount of bytes which is already present in system network buffers instead of blocking until all requested data will be recieved - Details here. So, one needs to use Read in a loop to get all requested data

  3. BinaryReader is known to throw an exception when getting less data from NetworkStream than it expected, instead of using a loop to retrieve the rest (and YES, I am sure, this means a bug!) - Details here

So, my solution was to partially reimplement BinaryReader (I have called my class BinReader) adding some useful features and making proper Read method with a loop:

public int Read( byte[] buf, int off, int count ) {
    int read = 0;
    while( read < count ) {
        int toread = count - read;
        int portion = BaseStream.Read( buf, off, toread );
        read += portion;
        off += portion;
    }
    return read;
}

That has solved it for me.

like image 71
mas.morozov Avatar answered Nov 05 '22 06:11

mas.morozov