c# Socket.AcceptAsync where is Socket.StopAcceptAsync?

252 Views Asked by At

There is AcceptAsync for Socket.

Code looks like this:

private Socket _listeningSocket;
private SocketAsyncEventArgs _socketEvent;

private Init()
{
   _socketEvent = new SocketAsyncEventArgs();
   _listeningSocket = ...; // Set up ip, port etc.
   _listeningSocket.Bind(...);
   _listeningSocket.Listen(...);
}

public void StartAccept()
{
   if (_socketEvent.AcceptSocket is not null)
   {
      _socketEvent.AcceptSocket = null;
   }

   if (!_listeningSocket.AcceptAsync(_socketEvent))
   {
       ProcessAccept(_socketEvent);
   }
}

public void StopAccept()
{
   // _listeningSocket.Stop or what ?
}

private void ProcessAccept(SocketAsyncEventArgs socketAsyncEvent)
{
   if (socketAsyncEvent.SocketError == SocketError.Success)
   {
        OnClientAccepted?.Invoke(this, socketAsyncEvent);
        StartAccept();
   }
}

How can I stop accepting and start again? I would love to have something like StopAcceptAsync... Or could you point me some example how to stop accepting with SocketAsyncEventArgs ?

0

There are 0 best solutions below