How to end a Socket.BeginAccept call

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();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top