Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / April 2007

Tip: Looking for answers? Try searching our database.

BeginAccept callback problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Clayton - 10 Apr 2007 09:47 GMT
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
Peter Duniho - 10 Apr 2007 18:34 GMT
> [...] 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
Clayton - 10 Apr 2007 21:11 GMT
On Apr 10, 7:34 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:

> > [...] Once I press the stop button an
> > ObjectDisposedException is fired. What is the reason for this? Is it
[quoted text clipped - 10 lines]
>
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.