BeginAccept callback problem

C

Clayton

Hi all,

I'm trying to develop a server that listens to incoming calls using
the asycnhronous methods BeginAccept / EndAccept. I start the server
(till this point it is ok) and few seconds later I stop it (with no
clients connected). Once I press the stop button an
ObjectDisposedException is fired. What is the reason for this? Is it
because the thread is still running even after I closed the server
socket? Here is the code:

private void btnStart_Click(object sender, EventArgs e)
{
btnStart.Enabled = false;
mniStart.Enabled = false;
btnStop.Enabled = true;
mniStop.Enabled = true;

try
{
sListener.BeginAccept(new
AsyncCallback(AcceptCallback), sListener);
lblStatus.Text = "Waiting for a connection...";
}
catch (SocketException se)
{
MessageBox.Show(se.Message, "VEP Server Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void btnStop_Click(object sender, EventArgs e)
{
btnStop.Enabled = false;
mniStop.Enabled = false;
btnStart.Enabled = true;
mniStart.Enabled = true;

try
{
lblStatus.Text = "Stopped";
//sListener.Shutdown(SocketShutdown.Both);
sListener.Close();
}
catch (SocketException se)
{
MessageBox.Show(se.Message, "VEP Server Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void AcceptCallback(IAsyncResult ar)
{
Socket listener = (Socket)ar.AsyncState;
Socket sHandler = listener.EndAccept(ar);
<---------------Exception fired here

MessageBox.Show("Connected with " +
sHandler.RemoteEndPoint.ToString());
}

Thanks in advance for your help,
Clayton
 
P

Peter Duniho

[...] Once I press the stop button an
ObjectDisposedException is fired. What is the reason for this? Is it
because the thread is still running even after I closed the server
socket?

Which thread?

When you close the socket, the accept that you started completes, causing
your callback to be called. Of course, you've closed the socket, so the
EndAccept results in the ObjectDisposedException which, as the
documentation for EndAccept points out, occurs when the socket has been
closed and you call EndAccept.

Pete
 
C

Clayton

[...] Once I press the stop button an
ObjectDisposedException is fired. What is the reason for this? Is it
because the thread is still running even after I closed the server
socket?

Which thread?

When you close the socket, the accept that you started completes, causing
your callback to be called. Of course, you've closed the socket, so the
EndAccept results in the ObjectDisposedException which, as the
documentation for EndAccept points out, occurs when the socket has been
closed and you call EndAccept.

Pete

OK thanks Pete. If managed to catch the ObjectDisposedException but
left it empty for now. I works but not the way I want. Still new to
socket programming.
Thanks again.

Regards,
Clayton
 

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