Re: Handling exceptions in Socket Callbacks

From: faktujaa (faktujaa_at_discussions.microsoft.com)
Date: 08/11/04


Date: Wed, 11 Aug 2004 09:11:04 -0700

Hi Koen,
Thanks for the help.
First of all, why im using this complicated method of socket communication -
actually what i understood from most of the articles is that synchronous way
of communication does not go well in performance as compared to asyncronous
in real world applications (multiple clients) as all the synchronous method
operates in the blocking mode. They have to poll for the data being received
and wait untill it is actually received whereas in asynchronous
communication, callback function will automatically notify the completion of
operation.
Secondly im using ManulaResetEvent to block the parent thread till the
callbak thread indicates its completion - this is nothing but im again using
synchronous way of communication (i agree with u) but microsoft has really
confused me by giving readymade asynchronous socket client example that im
using in my application. Please check the link -
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconnon-blockingclientsocketexample.asp.
Can u explain this to me as im now totally confused.
Lastly but the most impostant one say what if i use this example code
without ManualResetEvents then the code becomes truely asynchronous but still
there remains the problem of propogating the error generated in the callback
function as it is executed by a seperate thread. I know for sure that i have
to use asynchronous way of communication but how do i resolve this
problem????????
Now to give brief idea abt what im doing - Im using this client socket to
send print data to a third party print server - Hematrax.
Thanx in advance.

"RockinFewl" wrote:

> faktujaa,
>
> The exception propagation doesn't work because, in contrary to what you
> think, your Connect() method is NOT a parent function of your
> ConnectCallback(). The asynchronous Delegate mechanism uses an other
> thread to eventually run the callback on, so basically your two
> functions are highly independent, and surely not on the same call stack.
>
> In your case the solution is simple though.
> I see you use asynchronous methods, yet try to mimic a synchronous
> behaviour (with the mevtConnectDone event). This makes everything
> unnecessary complex, and induces also your particular problem. I don't
> know what your future plans with this code are, but I'd ditch the
> asynchronous approach here. You'd get this:
>
> private static void Connect(string strPrtrIPAddr, int intPrtrPort, ref
> socket rsocClient)
> {
> try
> {
> // Create remote end point.
> System.Net.IPAddress IPAddress = System.Net.IPAddress.Parse(strPrtrIPAddr);
> System.Net.IPEndPoint IPEndPoint = new System.Net.IPEndPoint(IPAddress,
> intPrtrPort);
>
> // Create a TCP/IP socket.
> rsocClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
> ProtocolType.Tcp);
>
> // Connect to the remote end point SYNCHRONOUSLY.
> rsocClient.Connect(IPEndPoint);
> }
>
> catch(Exception objException)
> {
> throw objException;
> }
>
> }
>
> No callback applies.
>
> Code goes untested, so forgive me the occasional typo.
>
> Hope his helps,
> Koen.
>
>
> faktujaa wrote:
>
> > Hi,
> > I am having some problem with callback used in socket implementation.
> > private static void Connect(string strPrtrIPAddr, int intPrtrPort, ref
> > Socket rsocClient)
> > {
> > try
> > {
> > // Create remote end point.
> > System.Net.IPAddress IPAddress = System.Net.IPAddress.Parse(strPrtrIPAddr);
> > System.Net.IPEndPoint IPEndPoint = new System.Net.IPEndPoint(IPAddress,
> > intPrtrPort);
> >
> > // Create a TCP/IP socket.
> > rsocClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
> > ProtocolType.Tcp);
> >
> > // Connect to the remote end point.
> > rsocClient.BeginConnect(IPEndPoint, new AsyncCallback(ConnectCallback),
> > rsocClient);
> >
> > // Signal the connection event to wait.
> > mevtConnectDone.WaitOne();
> > }
> > catch(Exception objException)
> > {
> > throw objException;
> > }
> > }
> >
> > private static void ConnectCallback(IAsyncResult IResult)
> > {
> > try
> > {
> > // Retrieve the socket from the state object.
> > Socket socClient = (Socket) IResult.AsyncState;
> >
> > // Complete the connection.
> > socClient.EndConnect(IResult);
> >
> > // Signal that the connection has been made.
> > mevtConnectDone.Set();
> > }
> > catch(Exception objException)
> > {
> > mevtConnectDone.Set();
> > throw objException;
> > }
> > }
> >
> > Please check the above two methods. Callback throws an error at EndConnect
> > but it is not propogated to the parent function. How to solve this problem.
> > Please help me asap. Thanks in advance.
>
>
> --
> Notice: Remove all packaging [from e-mail address] before use.
>



Relevant Pages

  • Re: Handling exceptions in Socket Callbacks
    ... You're correct the asynchronous socket methods give more opportunities ... I always handle exceptions in callbacks in the callback itself. ... why im using this complicated method of socket communication - ...
    (microsoft.public.dotnet.general)
  • Re: Question on Socket disconnect handling
    ... static void SendCallback ... // simulate timing that can cause callback ... SafeSocket socket = new SafeSocket( ... private class SendCallBackArgs ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: TCP Streams from Unknown source to VB.Net
    ... communicate with any types of other platforms on TCP/IP communication. ... the data structure is sent in binary format out over a socket. ... can't contain arrays. ... from the server. ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: Asynchronous socket operations and threadpool
    ... | 0 - You get the Socket.BeginRead callback, ... | an IOCP thread. ... You kick off the DB Request async, do as much more of the user request as ... An additional complication is that your socket is back in BeginRead mode so ...
    (microsoft.public.dotnet.framework)
  • Re: Question on Socket disconnect handling
    ... the common sense of not accessing resources after they've been released, ... callback is not a thread-safe way to determine whether the socket has ... ObjectDisposedException (not the one expected from the socket). ... new SendCallBackArgs( ...
    (microsoft.public.dotnet.languages.csharp)