Re: Handling exceptions in Socket Callbacks

From: RockinFewl (RockinBUBBLE_at_WRAPWhirlyWiryWeb.com)
Date: 08/12/04


Date: Thu, 12 Aug 2004 08:42:20 +0200


Hi faktujaa,

You're correct the asynchronous socket methods give more opportunities
in creating performant code. There aren't many occasions I use
synchronous ones myself.
However, I'd consider this particular Microsoft article as example code.
  Using an asynchrounous BeginConnect() to avoid blocking, yet
immediately do a WaitOne to _do_ blocking, doesn't make much sense.
Typically, you'd do other, useful things instead of just blocking
waiting for a completion event: you may attempt to begin some more
connections, or handle existing ones for instance.

Anyway, I see you feel intuitively your Connect() method can't possibly
be the place to catch exceptions thrown in ConnectCallback(). If you
remove the WaitOne in you original code, it's expectable Connect()
finishes BEFORE ConnectCallback() finishes (or even starts perhaps).
Again, this illustrates that the two method calls are not on the same
call stack.

I always handle exceptions in callbacks in the callback itself. Apart
from some complicated (unseen) approach to transfer exceptions as
objects to another thread and rethrowing it there, I see no other option.

Koen.

faktujaa wrote:
> 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.
>>

-- 
Notice: Remove all packaging [from e-mail address] before use.


Relevant Pages

  • Re: Handling exceptions in Socket Callbacks
    ... why im using this complicated method of socket communication - ... there remains the problem of propogating the error generated in the callback ... > catch(Exception objException) ...
    (microsoft.public.dotnet.general)
  • Re: Close a blocked socket
    ... from any other informational exceptions that occur in .NET. ... discards the socket, and the program code finds out about it the hard way. ... With 30 connections it ... I didn't "decide to go blocking". ...
    (microsoft.public.dotnet.languages.csharp)
  • 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: API Design: Is Socket.Select() seriously busted?
    ... outcomes-also causes inefficiencies because catching and handling exceptions ... directly from VS2005 help for Socket class: ... Win32 Winsock there was no need for using error trapping when responding to ...
    (microsoft.public.dotnet.languages.csharp)