Skip to content

Commit dcc2e40

Browse files
rzikmgewarren
andauthored
Update AsyncSockerServer example to fix possible StackOverflow (#8337)
* Update AsyncSockerServer to fix possible StackOverflow This reflects fixes from dotnet/runtime#74644 * Fix compilation * Update AsyncSocketServer.cs * Update snippets/csharp/System.Net.Sockets/SocketAsyncEventArgs/Overview/AsyncSocketServer.cs Co-authored-by: Genevieve Warren <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent 83a7d69 commit dcc2e40

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

snippets/csharp/System.Net.Sockets/SocketAsyncEventArgs/Overview/AsyncSocketServer.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ public void Start(IPEndPoint localEndPoint)
231231
listenSocket.Listen(100);
232232

233233
// post accepts on the listening socket
234-
StartAccept(null);
234+
SocketAsyncEventArgs acceptEventArg = new SocketAsyncEventArgs();
235+
acceptEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(AcceptEventArg_Completed);
236+
StartAccept(acceptEventArg);
235237

236238
//Console.WriteLine("{0} connected sockets with one outstanding receive posted to each....press any key", m_outstandingReadCount);
237239
Console.WriteLine("Press any key to terminate the server process....");
@@ -244,22 +246,19 @@ public void Start(IPEndPoint localEndPoint)
244246
// the accept operation on the server's listening socket</param>
245247
public void StartAccept(SocketAsyncEventArgs acceptEventArg)
246248
{
247-
if (acceptEventArg == null)
249+
// loop while the method completes synchronously
250+
bool willRaiseEvent = false;
251+
while (!willRaiseEvent)
248252
{
249-
acceptEventArg = new SocketAsyncEventArgs();
250-
acceptEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(AcceptEventArg_Completed);
251-
}
252-
else
253-
{
254-
// socket must be cleared since the context object is being reused
255-
acceptEventArg.AcceptSocket = null;
256-
}
253+
m_maxNumberAcceptedClients.WaitOne();
257254

258-
m_maxNumberAcceptedClients.WaitOne();
259-
bool willRaiseEvent = listenSocket.AcceptAsync(acceptEventArg);
260-
if (!willRaiseEvent)
261-
{
262-
ProcessAccept(acceptEventArg);
255+
// socket must be cleared since the context object is being reused
256+
acceptEventArg.AcceptSocket = null;
257+
willRaiseEvent = listenSocket.AcceptAsync(acceptEventArg);
258+
if (!willRaiseEvent)
259+
{
260+
ProcessAccept(acceptEventArg);
261+
}
263262
}
264263
}
265264

@@ -269,6 +268,9 @@ public void StartAccept(SocketAsyncEventArgs acceptEventArg)
269268
void AcceptEventArg_Completed(object sender, SocketAsyncEventArgs e)
270269
{
271270
ProcessAccept(e);
271+
272+
// Accept the next connection request
273+
StartAccept(e);
272274
}
273275

274276
private void ProcessAccept(SocketAsyncEventArgs e)
@@ -284,12 +286,10 @@ private void ProcessAccept(SocketAsyncEventArgs e)
284286

285287
// As soon as the client is connected, post a receive to the connection
286288
bool willRaiseEvent = e.AcceptSocket.ReceiveAsync(readEventArgs);
287-
if(!willRaiseEvent){
289+
if (!willRaiseEvent)
290+
{
288291
ProcessReceive(readEventArgs);
289292
}
290-
291-
// Accept the next connection request
292-
StartAccept(e);
293293
}
294294

295295
// This method is called whenever a receive or send operation is completed on a socket

0 commit comments

Comments
 (0)