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
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