473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Handling exceptions in Socket Callbacks

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
4 2233
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
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Aggelos I. Orfanakos | last post by:
Hello. In a program, I want to ensure that a socket closes (so I use try ... finally), but I also want to catch/handle a socket exception. This is what I have done: try: try: s = ... #...
0
by: Viktor Lundström | last post by:
Hi! I recently decided to write a streambuf which handles streamed IO to a device (ie. a socket). Now, in an effort to move away from C-style IO error handling (ie. if(read(..) == -1) ...), I...
3
by: Alberto Giménez | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi there! I'm doing some homework (almost finished), but now i'm reconsidering a design decission I made at the beginning. This is about error...
2
by: faktujaa | last post by:
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...
4
by: MadSage | last post by:
I currently have a multi-threaded server application with worker threads and a core thread. For all of these threads I have something like the following code: uint32 __stdcall...
10
by: John Nagle | last post by:
Here are three network-related exceptions. These were caught by "except" with no exception type, because none of the more specific exceptions matched. This is what a traceback produced: 1....
35
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
21
by: puzzlecracker | last post by:
Problem: I send a lot of requests to the application (running on a different box, of course), and I receive back responses from the app . Below: socket corresponds to Socket socket=new...
9
by: arnuld | last post by:
Can anyone point me to some online/offline document on error handling in C ? I am doing some Socket Programming in C and feeling a lots of difficult in error handling :( Also some time ago...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.