Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TcpClient.GetStream() sample code is incorrect and confusing #63154

Closed
geoffkizer opened this issue Dec 27, 2021 · 3 comments · Fixed by dotnet/dotnet-api-docs#7540
Closed

TcpClient.GetStream() sample code is incorrect and confusing #63154

geoffkizer opened this issue Dec 27, 2021 · 3 comments · Fixed by dotnet/dotnet-api-docs#7540
Labels
area-System.Net.Sockets documentation Documentation bug or enhancement, does not impact product or test code
Milestone

Comments

@geoffkizer
Copy link
Contributor

See https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient.getstream?view=net-6.0

There are several issues here:

(1) The sample incorrectly states twice that "Closing the tcpClient instance does not close the network stream." This is wrong: closing the TcpClient does in fact close the NetworkStream. The TcpClient.Close docs are clear about this.
(2) The sample calls GetStream() without having established the connection first. This will always fail.
(3) The sample explicitly tests for CanRead and CanWrite, but these will always be true on an established connection, so checking for these is very confusing.
(4) The sample explicitly closes the TcpClient in failure cases but not in the success case. This is bizarre and makes it unclear whether the user needs to call TcpClient.Close in general or not.
(5) The sample uses tcpClient.ReceiveBufferSize as the size of the user's allocated buffer for Receive. This seems to imply that this is good practice or even required; it is neither. TcpClient.ReceiveBufferSize configures the kernel buffer size for the socket and doesn't really have anything to do with the choice of user buffer size on Receive.

Additionally, the sample should be updated to use Span and using/IDisposable as any modern code would do.

If someone can tell me how to update the sample myself, I'd be happy to do so.

@geoffkizer geoffkizer added documentation Documentation bug or enhancement, does not impact product or test code area-System.Net.Sockets labels Dec 27, 2021
@ghost
Copy link

ghost commented Dec 27, 2021

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

See https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient.getstream?view=net-6.0

There are several issues here:

(1) The sample incorrectly states twice that "Closing the tcpClient instance does not close the network stream." This is wrong: closing the TcpClient does in fact close the NetworkStream. The TcpClient.Close docs are clear about this.
(2) The sample calls GetStream() without having established the connection first. This will always fail.
(3) The sample explicitly tests for CanRead and CanWrite, but these will always be true on an established connection, so checking for these is very confusing.
(4) The sample explicitly closes the TcpClient in failure cases but not in the success case. This is bizarre and makes it unclear whether the user needs to call TcpClient.Close in general or not.
(5) The sample uses tcpClient.ReceiveBufferSize as the size of the user's allocated buffer for Receive. This seems to imply that this is good practice or even required; it is neither. TcpClient.ReceiveBufferSize configures the kernel buffer size for the socket and doesn't really have anything to do with the choice of user buffer size on Receive.

Additionally, the sample should be updated to use Span and using/IDisposable as any modern code would do.

If someone can tell me how to update the sample myself, I'd be happy to do so.

Author: geoffkizer
Assignees: -
Labels:

documentation, area-System.Net.Sockets

Milestone: -

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Dec 27, 2021
@filipnavara
Copy link
Member

Did you mean this one: https://github.com/dotnet/dotnet-api-docs/blob/1dcbb0b6ec5614b59d15bd4aecb557ad07668fff/samples/snippets/csharp/VS_Snippets_Remoting/ClassicTcpClient.PublicMethodsAndPropertiesExample/CS/source.cs#L154-L198 ?

All the documentation is in that repository. You can click the "Edit" button in the upper right corner of the MSDN page and find the relevant method. The snippets are file links with "SnippetXY" tag. The files are in the same repository and each snippet is framed by a comment. You can submit PRs there.

@geoffkizer
Copy link
Contributor Author

Yes, that's what I was looking for, thanks @filipnavara!

@ghost ghost locked as resolved and limited conversation to collaborators Feb 4, 2022
@karelz karelz added this to the 7.0.0 milestone Apr 8, 2022
@karelz karelz removed the untriaged New issue has not been triaged by the area owner label Oct 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net.Sockets documentation Documentation bug or enhancement, does not impact product or test code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants