Jump to content
Xtreme .Net Talk

MNimmo

Members
  • Posts

    2
  • Joined

  • Last visited

MNimmo's Achievements

Newbie

Newbie (1/14)

0

Reputation

  1. As a quick update, it appears that EndAccept must be called within the asynchronous function but if the socket is invalid then you can call EndAccept within a trap.
  2. How to cancel asynchronous socket accept calls. Right firstly, Bucky. When you call close on an accepting socket, the asynchronous request is triggered as completed with an invalid socket, therefore when your program calls EndAccept it tries to access a non-existant socket and causes the EndAccept exception. As Rodenburg suggested a flag can be used to catch when this occurs so that you don't call EndAccept in these cases. However there is another way. The IAsyncResult parameter that is passed to the function contains the data about the socket that accepts the connection. I.e. A member of IAsyncResult contains the socket returned by EndAccept. This member is AsyncState. If you cast this to a socket, then you can look at the underlining handle for the socket to see if it is valid (>= 0) or invalid (-1). If the handle is -1 then the accept call was cancelled. Example.... private void AcceptSmtp(IAsyncResult ar) { Socket test = (Socket)ar.AsyncState; // Cast the result to a socket. if (test.Handle.ToInt32() == -1) // Check if the handle is invalid. { return; // Exit as the call was cancelled. } // If we get here then this is an actual accept. Socket client = smtpListener.EndAccept(ar); // Get the socket. ........ } Hope this helps you all, Mike Nimmo.
×
×
  • Create New...