dotnet / dotnet/dotnet-api-docs

StreamReader.Peek() blocks which not cited in documentation.

Open
#4,735 0 comments 0 reactions 1 assignee Assigned to @carlossanlop View on GitHub
area-System.IO Pri3 untriaged
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

StreamReader.Peek() blocks if the Stream is unused(empty).

I'm working on networking by using .Net.Sockets.

- Created StreamReader and StreamWriter for both Server and Client.
- Server and Client successfully connected.
- Loop for ```Receive()``` then ```Send()``` in thread A, loop for ```CheckInputAndCacheIt()``` in thread B, in both Server and Client
- Both receive method use ```if ( StreamReader.Peek() > -1 )``` to check if message arrived.
- Scenario 1 :
If ```NetworkStream.Write("Hello")``` in Client right after the connection is established,
the ```if ( StreamReader.Peek() > -1 )``` in Server does not block and the whole loop in thread A works fine.
- Scenario 2 :
If nothing is sent by Client through NetworkStream, the NetworkStream in Server is also unused(empty), entering the loop ends up being blocked and stuck at this ```if ( StreamReader.Peek() > -1 )``` line.

Not seeing any information about this in [https://docs.microsoft.com/en-us/dotnet/api/system.io.streamreader.peek?view=netcore-3.1](url). I think the blocking mode should be cited in the official documentation, or the use and status of ```StreamReader.Peek()```(or even ```Peek()``` in other stream types) would be vague.

Also found a blog(year 2005, .Net Framework 1.1) complaining this.(seems it was designed like this in the very beginning)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.