How to end a Socket.BeginAccept call

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When closing down my server I get the following exception


An unhandled exception of type 'System.InvalidOperationException' occurred
in system.dll

Additional information: AcceptCallback


I beleive that it is the asynchronous BeginAccept call which causes the
problem. How can I avoid this? Here is the code:

private void ListenForClients()
{
listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.Any, 9123));
listener.Listen(10);

listener.BeginAccept(new AsyncCallback(this.OnAccept), listener);
Invoke(new UpdateEventListDelegate(UpdateEventList), new object[] {
"Listening for clients..." } );
}

private void radioButtonStopped_CheckedChanged(object sender,
System.EventArgs e)
{
if (radioButtonStopped.Checked == true)
{
Invoke(new UpdateEventListDelegate(UpdateEventList), new object[] {
"Closing server..." } );
listener.Close();
listeningThread.Abort();
 
Back
Top