By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,965 Members | 1,457 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,965 IT Pros & Developers. It's quick & easy.

Handling exceptions in Socket Callbacks

P: n/a
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.
Jul 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
Jul 21 '05 #2

P: n/a
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.
Jul 21 '05 #3

P: n/a

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/de...ketexample.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.
Jul 21 '05 #4

P: n/a

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/de...ketexample.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.
Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.