Skip to content

Commit c07ca00

Browse files
rzikmgewarrenkarelz
authored
Make sure TcpClient is disposed in connect examples (dotnet#8460)
* Make sure TcpClient is disposed in connect examples * Use using statements in TcpListener example * Fix compilation * add vb project file * Update snippets/cpp/VS_Snippets_Remoting/System.Net.Sockets.TcpClient/CPP/tcpclient.cpp Co-authored-by: Karel Zikmund <[email protected]> Co-authored-by: Genevieve Warren <[email protected]> Co-authored-by: Karel Zikmund <[email protected]>
1 parent 8b7a4fc commit c07ca00

File tree

5 files changed

+63
-46
lines changed

5 files changed

+63
-46
lines changed

snippets/cpp/VS_Snippets_Remoting/System.Net.Sockets.TcpClient/CPP/tcpclient.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,28 @@ using namespace System::Security::Permissions;
3232
// <Snippet1>
3333
void Connect( String^ server, String^ message )
3434
{
35+
TcpClient^ client = nullptr;
3536
try
3637
{
3738
// Create a TcpClient.
3839
// Note, for this client to work you need to have a TcpServer
3940
// connected to the same address as specified by the server, port
4041
// combination.
4142
Int32 port = 13000;
42-
TcpClient^ client = gcnew TcpClient( server,port );
43+
client = gcnew TcpClient(server, port);
4344

4445
// Translate the passed message into ASCII and store it as a Byte array.
4546
array<Byte>^data = Text::Encoding::ASCII->GetBytes( message );
4647

4748
// Get a client stream for reading and writing.
48-
// Stream stream = client->GetStream();
49-
5049
NetworkStream^ stream = client->GetStream();
5150

5251
// Send the message to the connected TcpServer.
5352
stream->Write( data, 0, data->Length );
5453

5554
Console::WriteLine( "Sent: {0}", message );
5655

57-
// Receive the TcpServer::response.
56+
// Receive the server response.
5857

5958
// Buffer to store the response bytes.
6059
data = gcnew array<Byte>(256);
@@ -67,8 +66,10 @@ void Connect( String^ server, String^ message )
6766
responseData = Text::Encoding::ASCII->GetString( data, 0, bytes );
6867
Console::WriteLine( "Received: {0}", responseData );
6968

70-
// Close everything.
71-
client->Close();
69+
// Explicit close is not necessary since TcpClient::Dispose() will be
70+
// called automatically in finally block.
71+
// stream->Close();
72+
// client->Close();
7273
}
7374
catch ( ArgumentNullException^ e )
7475
{
@@ -78,6 +79,11 @@ void Connect( String^ server, String^ message )
7879
{
7980
Console::WriteLine( "SocketException: {0}", e );
8081
}
82+
finally
83+
{
84+
if (client != nullptr)
85+
delete client;
86+
}
8187

8288
Console::WriteLine( "\n Press Enter to continue..." );
8389
Console::Read();

snippets/csharp/System.Net.Sockets/TcpClient/Overview/tcpclient.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,22 @@ static void Connect(String server, String message)
6565
// connected to the same address as specified by the server, port
6666
// combination.
6767
Int32 port = 13000;
68-
TcpClient client = new TcpClient(server, port);
68+
69+
// Prefer using declaration to ensure the instance is Disposed later.
70+
using TcpClient client = new TcpClient(server, port);
6971

7072
// Translate the passed message into ASCII and store it as a Byte array.
7173
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
7274

7375
// Get a client stream for reading and writing.
74-
// Stream stream = client.GetStream();
75-
7676
NetworkStream stream = client.GetStream();
7777

7878
// Send the message to the connected TcpServer.
7979
stream.Write(data, 0, data.Length);
8080

8181
Console.WriteLine("Sent: {0}", message);
8282

83-
// Receive the TcpServer.response.
83+
// Receive the server response.
8484

8585
// Buffer to store the response bytes.
8686
data = new Byte[256];
@@ -93,9 +93,10 @@ static void Connect(String server, String message)
9393
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
9494
Console.WriteLine("Received: {0}", responseData);
9595

96-
// Close everything.
97-
stream.Close();
98-
client.Close();
96+
// Explicit close is not necessary since TcpClient.Dispose() will be
97+
// called automatically.
98+
// stream.Close();
99+
// client.Close();
99100
}
100101
catch (ArgumentNullException e)
101102
{

snippets/csharp/System.Net.Sockets/TcpListener/Overview/tcpserver.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MyTcpListener
2424
{
2525
public static void Main()
2626
{
27-
TcpListener server=null;
27+
TcpListener server = null;
2828
try
2929
{
3030
// Set the TcpListener on port 13000.
@@ -48,7 +48,7 @@ public static void Main()
4848

4949
// Perform a blocking call to accept requests.
5050
// You could also use server.AcceptSocket() here.
51-
TcpClient client = server.AcceptTcpClient();
51+
using TcpClient client = server.AcceptTcpClient();
5252
Console.WriteLine("Connected!");
5353

5454
data = null;
@@ -75,7 +75,7 @@ public static void Main()
7575
Console.WriteLine("Sent: {0}", data);
7676
}
7777

78-
// Shutdown and end connection
78+
// Shutdown and end the connection
7979
client.Close();
8080
}
8181
}
@@ -85,8 +85,7 @@ public static void Main()
8585
}
8686
finally
8787
{
88-
// Stop listening for new clients.
89-
server.Stop();
88+
server.Stop();
9089
}
9190

9291
Console.WriteLine("\nHit enter to continue...");
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net6.0</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
</Project>

snippets/visualbasic/VS_Snippets_Remoting/System.Net.Sockets.TcpClient/VB/tcpclient.vb

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,38 @@ Class MyTcpClient
4343
' connected to the same address as specified by the server, port
4444
' combination.
4545
Dim port As Int32 = 13000
46-
Dim client As New TcpClient(server, port)
47-
48-
' Translate the passed message into ASCII and store it as a Byte array.
49-
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
50-
51-
' Get a client stream for reading and writing.
52-
' Stream stream = client.GetStream();
53-
Dim stream As NetworkStream = client.GetStream()
54-
55-
' Send the message to the connected TcpServer.
56-
stream.Write(data, 0, data.Length)
57-
58-
Console.WriteLine("Sent: {0}", message)
59-
60-
' Receive the TcpServer.response.
61-
' Buffer to store the response bytes.
62-
data = New [Byte](256) {}
63-
64-
' String to store the response ASCII representation.
65-
Dim responseData As [String] = [String].Empty
66-
67-
' Read the first batch of the TcpServer response bytes.
68-
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
69-
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
70-
Console.WriteLine("Received: {0}", responseData)
46+
47+
' Prefer using declaration to ensure the instance is Disposed later.
48+
Using client As New TcpClient(server, port)
7149

72-
' Close everything.
73-
stream.Close()
74-
client.Close()
50+
' Translate the passed message into ASCII and store it as a Byte array.
51+
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
52+
53+
' Get a client stream for reading and writing.
54+
Dim stream As NetworkStream = client.GetStream()
55+
56+
' Send the message to the connected TcpServer.
57+
stream.Write(data, 0, data.Length)
58+
59+
Console.WriteLine("Sent: {0}", message)
60+
61+
' Receive the server response.
62+
' Buffer to store the response bytes.
63+
data = New [Byte](256) {}
64+
65+
' String to store the response ASCII representation.
66+
Dim responseData As [String] = [String].Empty
67+
68+
' Read the first batch of the TcpServer response bytes.
69+
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
70+
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
71+
Console.WriteLine("Received: {0}", responseData)
72+
73+
' Explicit close is not necessary since TcpClient.Dispose() will be
74+
' called automatically.
75+
' stream.Close()
76+
' client.Close()
77+
End Using
7578
Catch e As ArgumentNullException
7679
Console.WriteLine("ArgumentNullException: {0}", e)
7780
Catch e As SocketException

0 commit comments

Comments
 (0)