Skip to content

Commit ae9da77

Browse files
authored
Fix dotnet#7415 System.Net.Sockets.Socket.Receive code example does not work. (dotnet#7417)
1 parent 8ae720f commit ae9da77

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed
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+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

samples/snippets/csharp/VS_Snippets_Remoting/Socket_Sync_Send_Receive/CS/source.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,9 @@ public static int SendReceiveTest3(Socket server)
104104
Console.WriteLine("Sent {0} bytes.", i);
105105

106106
// Get reply from the server.
107-
int byteCount = server.Receive(bytes, server.Available,
108-
SocketFlags.None);
107+
int byteCount = server.Receive(bytes, bytes.Length, SocketFlags.None);
109108
if (byteCount > 0)
110-
Console.WriteLine(Encoding.UTF8.GetString(bytes));
109+
Console.WriteLine(Encoding.UTF8.GetString(bytes, 0, byteCount));
111110
}
112111
catch (SocketException e)
113112
{
@@ -132,11 +131,10 @@ public static int SendReceiveTest4(Socket server)
132131
Console.WriteLine("Sent {0} bytes.", byteCount);
133132

134133
// Get reply from the server.
135-
byteCount = server.Receive(bytes, 0, server.Available,
136-
SocketFlags.None);
134+
byteCount = server.Receive(bytes, 0, bytes.Length, SocketFlags.None);
137135

138136
if (byteCount > 0)
139-
Console.WriteLine(Encoding.UTF8.GetString(bytes));
137+
Console.WriteLine(Encoding.UTF8.GetString(bytes, 0, byteCount));
140138
}
141139
catch (SocketException e)
142140
{
@@ -182,7 +180,7 @@ public static void SendTo2()
182180
s.Close();
183181
}
184182
//</Snippet6>
185-
//<Snippet7>
183+
//<Snippet7>
186184
public static void SendTo3()
187185
{
188186
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
@@ -238,7 +236,7 @@ public static void ReceiveFrom1()
238236
s.Bind(endPoint);
239237

240238
byte[] msg = new Byte[256];
241-
Console.WriteLine ("Waiting to receive datagrams from client...");
239+
Console.WriteLine("Waiting to receive datagrams from client...");
242240

243241
// This call blocks.
244242
s.ReceiveFrom(msg, ref senderRemote);
@@ -264,13 +262,13 @@ public static void ReceiveFrom2()
264262
s.Bind(endPoint);
265263

266264
byte[] msg = new Byte[256];
267-
Console.WriteLine ("Waiting to receive datagrams from client...");
265+
Console.WriteLine("Waiting to receive datagrams from client...");
268266
// This call blocks.
269267
s.ReceiveFrom(msg, SocketFlags.None, ref senderRemote);
270268
s.Close();
271269
}
272270
//</Snippet10>
273-
//<Snippet11>
271+
//<Snippet11>
274272
public static void ReceiveFrom3()
275273
{
276274
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
@@ -288,7 +286,7 @@ public static void ReceiveFrom3()
288286
s.Bind(endPoint);
289287

290288
byte[] msg = new Byte[256];
291-
Console.WriteLine ("Waiting to receive datagrams from client...");
289+
Console.WriteLine("Waiting to receive datagrams from client...");
292290
// This call blocks.
293291
s.ReceiveFrom(msg, msg.Length, SocketFlags.None, ref senderRemote);
294292
s.Close();
@@ -311,7 +309,7 @@ public static void ReceiveFrom4()
311309
// Binding is required with ReceiveFrom calls.
312310
s.Bind(endPoint);
313311
byte[] msg = new Byte[256];
314-
Console.WriteLine ("Waiting to receive datagrams from client...");
312+
Console.WriteLine("Waiting to receive datagrams from client...");
315313
// This call blocks.
316314
s.ReceiveFrom(msg, 0, msg.Length, SocketFlags.None, ref senderRemote);
317315
s.Close();
@@ -363,13 +361,13 @@ public static void RunUdpTests()
363361
// To test tcp - run 2 instances source /s runs server, source /c runs client.
364362
// To test Upd run source /u.
365363

366-
public static int Main(string[] args)
364+
public static int Main(string[] args)
367365
{
368366
string host;
369367
bool isServer;
370368

371369
char c = args[0].ToLower()[1];
372-
if ( c == 'c')
370+
if (c == 'c')
373371
{
374372
isServer = false;
375373
host = "127.0.0.1";
@@ -404,7 +402,7 @@ public static int Main(string[] args)
404402
{
405403
sender = s.Accept();
406404
// exchange messages with all clients tests
407-
for (int i = 0; i< 4; i++)
405+
for (int i = 0; i < 4; i++)
408406
{
409407
ReceiveTest1(sender);
410408
}

0 commit comments

Comments
 (0)