|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
BeginAccept callback problemI'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 On Tue, 10 Apr 2007 01:47:49 -0700, Clayton <clayton.cu***@gmail.com>
wrote: > [...] Once I press the stop button an Which thread?> ObjectDisposedException is fired. What is the reason for this? Is it > because the thread is still running even after I closed the server > socket? 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
Show quote
On Apr 10, 7:34 pm, "Peter Duniho" <NpOeStPe***@nnowslpianmk.com> OK thanks Pete. If managed to catch the ObjectDisposedException butwrote: > On Tue, 10 Apr 2007 01:47:49 -0700, Clayton <clayton.cu***@gmail.com> > wrote: > > > [...] 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 left it empty for now. I works but not the way I want. Still new to socket programming. Thanks again. Regards, Clayton |
|||||||||||||||||||||||