Skip to content

Commit 1d02688

Browse files
authored
Improve NetworkStream remarks and samples (#8537)
Addressing a couple of issues pointed out in #7609 and #1899: - DataAvailable should not be used to detect the end of transmission. - Scrapped code sample from DataAvailable docs entirely. It's a simple bool property. - Added a [!NOTE] on DataAvailable - Changed the Read example to read data until EOF is reached instead instead of watching DataAvailable. Scrapped C++/CLI and VB examples for Read, maintaining them is too much effort with little benefit. - Scrap dumb examples on CanRead, CanWrite and constructors. Improve some of the remarks.
1 parent c1e7e4f commit 1d02688

File tree

4 files changed

+83
-174
lines changed
  • snippets
    • cpp/VS_Snippets_Remoting/NetworkStream_Synch_SendAndReceive/CPP
    • csharp/System.Net.Sockets/NetworkStream/Overview
    • visualbasic/VS_Snippets_Remoting/NetworkStream_Synch_SendAndReceive/VB
  • xml/System.Net.Sockets

4 files changed

+83
-174
lines changed

snippets/cpp/VS_Snippets_Remoting/NetworkStream_Synch_SendAndReceive/CPP/source.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ using namespace System::Threading;
1414

1515
void MySample( bool networkStreamOwnsSocket )
1616
{
17-
//<Snippet1>
18-
// This should be the classwide example.
1917
// Create a socket and connect with a remote host.
2018
IPHostEntry^ myIpHostEntry = Dns::GetHostEntry( "www.contoso.com" );
2119
IPEndPoint^ myIpEndPoint = gcnew IPEndPoint( myIpHostEntry->AddressList[ 0 ],1001 );
@@ -27,11 +25,7 @@ void MySample( bool networkStreamOwnsSocket )
2725
{
2826
mySocket->Connect( myIpEndPoint );
2927

30-
//<Snippet2>
31-
// Examples for constructors that do not specify file permission.
32-
// Create the NetworkStream for communicating with the remote host.
3328
NetworkStream^ myNetworkStream;
34-
3529
if ( networkStreamOwnsSocket )
3630
{
3731
myNetworkStream = gcnew NetworkStream( mySocket,true );
@@ -40,7 +34,6 @@ void MySample( bool networkStreamOwnsSocket )
4034
{
4135
myNetworkStream = gcnew NetworkStream( mySocket );
4236
}
43-
//</Snippet2>
4437

4538
//<Snippet3>
4639
// Examples for CanWrite, and CanWrite
@@ -57,7 +50,6 @@ void MySample( bool networkStreamOwnsSocket )
5750
}
5851
//</Snippet3>
5952

60-
//<Snippet4>
6153
// Examples for CanRead, Read, and DataAvailable.
6254
// Check to see if this NetworkStream is readable.
6355
if ( myNetworkStream->CanRead )
@@ -84,7 +76,6 @@ void MySample( bool networkStreamOwnsSocket )
8476
{
8577
Console::WriteLine( "Sorry. You cannot read from this NetworkStream." );
8678
}
87-
//</Snippet4>
8879

8980
//<Snippet5>
9081
// Example for closing the NetworkStream.
@@ -98,7 +89,6 @@ void MySample( bool networkStreamOwnsSocket )
9889
Console::WriteLine( "Exception Thrown: {0}", exception->ToString() );
9990
}
10091
}
101-
//</Snippet1>
10292

10393
int main( int argc, char *argv[] )
10494
{

snippets/csharp/System.Net.Sockets/NetworkStream/Overview/source.cs

Lines changed: 59 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,82 +5,63 @@
55

66
using System;
77
using System.Text;
8-
using System.IO;
98
using System.Net;
109
using System.Net.Sockets;
11-
using System.Threading;
1210

13-
public class NetworkStream_Sync_Send_Receive{
11+
public class NetworkStream_Sync_Send_Receive
12+
{
13+
public static void MySample(bool networkStreamOwnsSocket)
14+
{
1415

15-
public static void MySample(bool networkStreamOwnsSocket){
16-
17-
//<Snippet1>
18-
// This should be the classwide example.
16+
//<Snippet1>
17+
// This should be the classwide example.
1918

20-
// Create a socket and connect with a remote host.
21-
IPHostEntry myIpHostEntry = Dns.GetHostEntry("www.contoso.com");
22-
IPEndPoint myIpEndPoint = new IPEndPoint(myIpHostEntry.AddressList[0], 1001);
19+
// Create a socket and connect with a remote host.
20+
IPHostEntry myIpHostEntry = Dns.GetHostEntry("www.contoso.com");
21+
IPEndPoint myIpEndPoint = new IPEndPoint(myIpHostEntry.AddressList[0], 1001);
2322

24-
Socket mySocket = new Socket(myIpEndPoint.Address.AddressFamily,
25-
SocketType.Stream,
26-
ProtocolType.Tcp);
27-
try{
23+
Socket mySocket = new Socket(myIpEndPoint.Address.AddressFamily,
24+
SocketType.Stream,
25+
ProtocolType.Tcp);
26+
try
27+
{
2828
mySocket.Connect(myIpEndPoint);
2929

30-
//<Snippet2>
31-
// Examples for constructors that do not specify file permission.
32-
3330
// Create the NetworkStream for communicating with the remote host.
34-
NetworkStream myNetworkStream;
35-
36-
if (networkStreamOwnsSocket){
37-
myNetworkStream = new NetworkStream(mySocket, true);
38-
}
39-
else{
40-
myNetworkStream = new NetworkStream(mySocket);
41-
}
42-
//</Snippet2>
31+
NetworkStream myNetworkStream = new NetworkStream(mySocket, networkStreamOwnsSocket);
4332

4433
//<Snippet3>
4534
// Examples for CanWrite, and CanWrite
46-
4735
// Check to see if this NetworkStream is writable.
48-
if (myNetworkStream.CanWrite){
49-
50-
byte[] myWriteBuffer = Encoding.ASCII.GetBytes("Are you receiving this message?");
51-
myNetworkStream.Write(myWriteBuffer, 0, myWriteBuffer.Length);
36+
if (myNetworkStream.CanWrite)
37+
{
38+
byte[] myWriteBuffer = Encoding.ASCII.GetBytes("Are you receiving this message?");
39+
myNetworkStream.Write(myWriteBuffer, 0, myWriteBuffer.Length);
5240
}
53-
else{
54-
Console.WriteLine("Sorry. You cannot write to this NetworkStream.");
41+
else
42+
{
43+
Console.WriteLine("Sorry. You cannot write to this NetworkStream.");
5544
}
5645

5746
//</Snippet3>
5847

48+
// Examples for Read.
5949
//<Snippet4>
60-
// Examples for CanRead, Read, and DataAvailable.
61-
62-
// Check to see if this NetworkStream is readable.
63-
if(myNetworkStream.CanRead){
64-
byte[] myReadBuffer = new byte[1024];
65-
StringBuilder myCompleteMessage = new StringBuilder();
66-
int numberOfBytesRead = 0;
67-
68-
// Incoming message may be larger than the buffer size.
69-
do{
70-
numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
71-
72-
myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
73-
}
74-
while(myNetworkStream.DataAvailable);
75-
76-
// Print out the received message to the console.
77-
Console.WriteLine("You received the following message : " +
78-
myCompleteMessage);
79-
}
80-
else{
81-
Console.WriteLine("Sorry. You cannot read from this NetworkStream.");
50+
byte[] myReadBuffer = new byte[1024];
51+
StringBuilder myCompleteMessage = new StringBuilder();
52+
int numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
53+
54+
// Read all the data until the end of stream has been reached.
55+
// The incoming message may be larger than the buffer size.
56+
while (numberOfBytesRead > 0)
57+
{
58+
myCompleteMessage.Append(Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
59+
numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
8260
}
8361

62+
// Print out the received message to the console.
63+
Console.WriteLine("You received the following message : " + myCompleteMessage);
64+
8465
//</Snippet4>
8566

8667
//<Snippet5>
@@ -90,24 +71,30 @@ public static void MySample(bool networkStreamOwnsSocket){
9071
myNetworkStream.Close();
9172

9273
//</Snippet5>
93-
}
94-
catch (Exception exception){
74+
}
75+
catch (Exception exception)
76+
{
9577
Console.WriteLine("Exception Thrown: " + exception.ToString());
96-
}
97-
}
98-
99-
//</Snippet1>
100-
101-
public static void Main(String[] args){
102-
if (args[0] == "yes"){
103-
NetworkStream_Sync_Send_Receive.MySample(true);
104-
}
105-
else if (args[0] == "no"){
106-
NetworkStream_Sync_Send_Receive.MySample(false);
78+
}
10779
}
108-
else{
109-
Console.WriteLine("Must use 'yes' to allow the NetworkStream to own the Socket or " +
110-
"\n 'no' to prohibit NetworkStream from owning the Socket. ");
80+
81+
//</Snippet1>
82+
83+
public static void Main(String[] args)
84+
{
85+
MySample(true);
86+
if (args[0] == "yes")
87+
{
88+
NetworkStream_Sync_Send_Receive.MySample(true);
89+
}
90+
else if (args[0] == "no")
91+
{
92+
NetworkStream_Sync_Send_Receive.MySample(false);
93+
}
94+
else
95+
{
96+
Console.WriteLine("Must use 'yes' to allow the NetworkStream to own the Socket or " +
97+
"\n 'no' to prohibit NetworkStream from owning the Socket. ");
98+
}
11199
}
112100
}
113-
}

snippets/visualbasic/VS_Snippets_Remoting/NetworkStream_Synch_SendAndReceive/VB/source.vb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Public Class NetworkStream_Sync_Send_Receive
1313

1414
Public Shared Sub MySample(networkStreamOwnsSocket As Boolean)
1515

16-
'<Snippet1>
1716
' This should be the classwide example.
1817
' Create a socket and connect with a remote host.
1918
Dim myIpHostEntry As IPHostEntry = Dns.GetHostEntry("www.contoso.com")
@@ -23,7 +22,6 @@ Public Class NetworkStream_Sync_Send_Receive
2322
Try
2423
mySocket.Connect(myIpEndPoint)
2524

26-
'<Snippet2>
2725
' Examples for constructors that do not specify file permission.
2826
' Create the NetworkStream for communicating with the remote host.
2927
Dim myNetworkStream As NetworkStream
@@ -33,7 +31,6 @@ Public Class NetworkStream_Sync_Send_Receive
3331
Else
3432
myNetworkStream = New NetworkStream(mySocket)
3533
End If
36-
'</Snippet2>
3734

3835
'<Snippet3>
3936
' Examples for CanWrite, and CanWrite
@@ -48,7 +45,6 @@ Public Class NetworkStream_Sync_Send_Receive
4845

4946
'</Snippet3>
5047

51-
'<Snippet4>
5248
' Examples for CanRead, Read, and DataAvailable.
5349
' Check to see if this NetworkStream is readable.
5450
If myNetworkStream.CanRead Then
@@ -67,8 +63,6 @@ Public Class NetworkStream_Sync_Send_Receive
6763
Else
6864
Console.WriteLine("Sorry. You cannot read from this NetworkStream.")
6965
End If
70-
71-
'</Snippet4>
7266

7367
'<Snippet5>
7468
' Example for closing the NetworkStream.
@@ -86,8 +80,6 @@ Public Class NetworkStream_Sync_Send_Receive
8680
Main(System.Environment.GetCommandLineArgs())
8781
End Sub
8882

89-
90-
'</Snippet1>
9183
Overloads Public Shared Sub Main(args() As [String])
9284
If args(0) = "yes" Then
9385
NetworkStream_Sync_Send_Receive.MySample(True)

0 commit comments

Comments
 (0)