473,402 Members | 2,055 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,402 software developers and data experts.

Raising an exception in a Sockets Async Callback Problem.

Hi,

Ive been trying to work this out for the past 2 days now and im not
getting anywhere fast.

The problem i have is that i am using Asynchronous sockets to create a
Socket Client library. When i try to connect to a server that doesnt
exist it raises a "Connection forcibly rejected by the resmote host"
SocketException.

Because this is thrown inside an Async Callback it is not "bubbling up"
the call stack so the user can handle it so I end up with an
UnhandledException error.

Because of the nature of the exception message i need to report this so
that the user of the client can handle this themselves (by using a
messagebox or whatever they want to do).

Does anyone know how i can cause this exception to be thrown in the
method that calls BeginInvoke() so i can bubble it back up to the user
to handle themselves in their app?

My code for this block is as follows:
=====================================

/// <summary>
/// Begins a aynchronous connection operation and delegates a async
/// callback so we don't lock up resources while waiting for the
/// operation to complete.
/// </summary>
public void Connect()
{
if(m_SocketClient != null)
{
try
{
m_SocketState = SocketState.Connecting;

// Begin the asynchronous connection to the
// host.

m_SocketClient.BeginConnect(m_ipEndPoint, new
AsyncCallback(ConnectCallBack), m_SocketClient);
}
catch(SocketException se)
{
m_SocketState = SocketState.Error;

throw new ClientException("An error has occured
while trying to connect to the remote host.",
se);
}
}
else
{
m_SocketState = SocketState.Error;

throw new ClientException("Cannot open connection when
the socket is uninitiated.");
}
}
----------------------------------------------------------------------
/// <summary>
/// Asynchronous callback for Connect method.
/// </summary>
/// <param name="asyn">Status of the asynchronous operation.</param>
private void ConnectCallBack(IAsyncResult asyn)
{
try
{
// End connection procedure.
m_SocketClient.EndConnect(asyn);

if(m_SocketClient.Connected)
{
m_SocketState = SocketState.Connected;

// Begin receiving data.
ReceiveData();

// Check to see if the event handler exists
if(OnSocketConnected != null)
{
// Create event args
ClientEventArgs args = new
ClientEventArgs(m_SocketClient);
args.SocketID = m_SocketID;
args.RemoteHost = m_HostAddress;
args.RemoteIP = m_HostIP;
args.RemotePort = m_Port;
args.State = m_SocketState;

// Raise the Connected event.
OnSocketConnected(this, args);
}
}
}
catch(SocketException se)
{
m_SocketState = SocketState.Error;

throw new ClientException("An Error occured while
attempting to end connection process with remote host.",
se);
}
}

Cheers,

David
Nov 15 '05 #1
3 5090
Why don't you just always save the exception in a variable or so inside your
class that connects.. then the caller class can check for errors afterwards.

Another way you can go is that you may like to fire an event in case there
is some error out there.

"David" <u0***@csc.liv.ac.uk> wrote in message
news:10****************@despina.uk.clara.net...
Hi,

Ive been trying to work this out for the past 2 days now and im not
getting anywhere fast.

The problem i have is that i am using Asynchronous sockets to create a
Socket Client library. When i try to connect to a server that doesnt
exist it raises a "Connection forcibly rejected by the resmote host"
SocketException.

Because this is thrown inside an Async Callback it is not "bubbling up"
the call stack so the user can handle it so I end up with an
UnhandledException error.

Because of the nature of the exception message i need to report this so
that the user of the client can handle this themselves (by using a
messagebox or whatever they want to do).

Does anyone know how i can cause this exception to be thrown in the
method that calls BeginInvoke() so i can bubble it back up to the user
to handle themselves in their app?

My code for this block is as follows:
=====================================

/// <summary>
/// Begins a aynchronous connection operation and delegates a async
/// callback so we don't lock up resources while waiting for the
/// operation to complete.
/// </summary>
public void Connect()
{
if(m_SocketClient != null)
{
try
{
m_SocketState = SocketState.Connecting;

// Begin the asynchronous connection to the
// host.

m_SocketClient.BeginConnect(m_ipEndPoint, new
AsyncCallback(ConnectCallBack), m_SocketClient);
}
catch(SocketException se)
{
m_SocketState = SocketState.Error;

throw new ClientException("An error has occured
while trying to connect to the remote host.",
se);
}
}
else
{
m_SocketState = SocketState.Error;

throw new ClientException("Cannot open connection when
the socket is uninitiated.");
}
}
----------------------------------------------------------------------
/// <summary>
/// Asynchronous callback for Connect method.
/// </summary>
/// <param name="asyn">Status of the asynchronous operation.</param>
private void ConnectCallBack(IAsyncResult asyn)
{
try
{
// End connection procedure.
m_SocketClient.EndConnect(asyn);

if(m_SocketClient.Connected)
{
m_SocketState = SocketState.Connected;

// Begin receiving data.
ReceiveData();

// Check to see if the event handler exists
if(OnSocketConnected != null)
{
// Create event args
ClientEventArgs args = new
ClientEventArgs(m_SocketClient);
args.SocketID = m_SocketID;
args.RemoteHost = m_HostAddress;
args.RemoteIP = m_HostIP;
args.RemotePort = m_Port;
args.State = m_SocketState;

// Raise the Connected event.
OnSocketConnected(this, args);
}
}
}
catch(SocketException se)
{
m_SocketState = SocketState.Error;

throw new ClientException("An Error occured while
attempting to end connection process with remote host.",
se);
}
}

Cheers,

David

Nov 15 '05 #2
The socket library is going to be a generic one for all the apps i and
other people write so i dont have to keep messing with sockets so
handling it with a variable like that isnt an option as it would involve
manual checking which is a little messy.

Having it fire an event would make it get handled in the wrong place in
the consuming program, i need to be able to catch the exception in a try
/ catch block around the place where the user instantiates a client and
connects.
David

hOSAM wrote:
Why don't you just always save the exception in a variable or so inside your
class that connects.. then the caller class can check for errors afterwards.

Another way you can go is that you may like to fire an event in case there
is some error out there.

"David" <u0***@csc.liv.ac.uk> wrote in message
news:10****************@despina.uk.clara.net...
Hi,

Ive been trying to work this out for the past 2 days now and im not
getting anywhere fast.

The problem i have is that i am using Asynchronous sockets to create a
Socket Client library. When i try to connect to a server that doesnt
exist it raises a "Connection forcibly rejected by the resmote host"
SocketException.

Because this is thrown inside an Async Callback it is not "bubbling up"
the call stack so the user can handle it so I end up with an
UnhandledException error.

Because of the nature of the exception message i need to report this so
that the user of the client can handle this themselves (by using a
messagebox or whatever they want to do).

Does anyone know how i can cause this exception to be thrown in the
method that calls BeginInvoke() so i can bubble it back up to the user
to handle themselves in their app?

My code for this block is as follows:
=====================================

/// <summary>
/// Begins a aynchronous connection operation and delegates a async
/// callback so we don't lock up resources while waiting for the
/// operation to complete.
/// </summary>
public void Connect()
{
if(m_SocketClient != null)
{
try
{
m_SocketState = SocketState.Connecting;

// Begin the asynchronous connection to the
// host.

m_SocketClient.BeginConnect(m_ipEndPoint, new
AsyncCallback(ConnectCallBack), m_SocketClient);
}
catch(SocketException se)
{
m_SocketState = SocketState.Error;

throw new ClientException("An error has occured
while trying to connect to the remote host.",
se);
}
}
else
{
m_SocketState = SocketState.Error;

throw new ClientException("Cannot open connection when
the socket is uninitiated.");
}
}
----------------------------------------------------------------------
/// <summary>
/// Asynchronous callback for Connect method.
/// </summary>
/// <param name="asyn">Status of the asynchronous operation.</param>
private void ConnectCallBack(IAsyncResult asyn)
{
try
{
// End connection procedure.
m_SocketClient.EndConnect(asyn);

if(m_SocketClient.Connected)
{
m_SocketState = SocketState.Connected;

// Begin receiving data.
ReceiveData();

// Check to see if the event handler exists
if(OnSocketConnected != null)
{
// Create event args
ClientEventArgs args = new
ClientEventArgs(m_SocketClient);
args.SocketID = m_SocketID;
args.RemoteHost = m_HostAddress;
args.RemoteIP = m_HostIP;
args.RemotePort = m_Port;
args.State = m_SocketState;

// Raise the Connected event.
OnSocketConnected(this, args);
}
}
}
catch(SocketException se)
{
m_SocketState = SocketState.Error;

throw new ClientException("An Error occured while
attempting to end connection process with remote host.",
se);
}
}

Cheers,

David


Nov 15 '05 #3
You don't have to show all the mess to the user..
It is really that simple as I see it:

I understand that you want to "Block and wait" if there will be any
exceptions on connection operation... because you simply want to throw
exceptions from the function that is seen by the user.

So that means you have to wait *for at least some time* until the
EndConnect() throws the exception that you are after..
something that might smell like this:

// Here it is:
Exception myException = null;
public void Connect(){
if(m_SocketClient != null)
{
try
{
m_SocketState = SocketState.Connecting;
// Begin the asynchronous connection to the host.
m_SocketClient.BeginConnect(m_ipEndPoint, new
AsyncCallback(ConnectCallBack), m_SocketClient);
while( m_SocketState == SocketState.Connecting )
{
System.Threading.Thread.Sleep(10);
}
if ( m_SocketState == SocketState.Error )
{
throw myException;
}

}
catch(SocketException se)
{
m_SocketState = SocketState.Error;
throw new ClientException("An error has occured while trying to connect to
the remote host.", se)
}
}

else
{
m_SocketState = SocketState.Error;
throw new ClientException("Cannot open connection when the socket is
uninitiated.") }
}

// <summary>
/// Asynchronous callback for Connect method.
/// </summary>
/// <param name="asyn">Status of the asynchronous operation.</param>
private void ConnectCallBack(IAsyncResult asyn)
{
try
{
// End connection procedure.
m_SocketClient.EndConnect(asyn);
if(m_SocketClient.Connected)
{
m_SocketState = SocketState.Connected;
// Begin receiving data.
ReceiveData();
// Check to see if the event handler exists
if(OnSocketConnected != null)
{
// Create event args
ClientEventArgs args = new
ClientEventArgs(m_SocketClient);
args.SocketID = m_SocketID;
args.RemoteHost = m_HostAddress;
args.RemoteIP = m_HostIP;
args.RemotePort = m_Port;
args.State = m_SocketState;
// Raise the Connected event.
OnSocketConnected(this, args);
}

}

}
catch(SocketException se)
{
m_SocketState = SocketState.Error;
// keep it with you.
myException = ClientException("An Error occured while attempting to end
connection process with remote host.", se);
}
}


"David" <u0***@csc.liv.ac.uk> wrote in message
news:10****************@dyke.uk.clara.net...
The socket library is going to be a generic one for all the apps i and
other people write so i dont have to keep messing with sockets so
handling it with a variable like that isnt an option as it would involve
manual checking which is a little messy.

Having it fire an event would make it get handled in the wrong place in
the consuming program, i need to be able to catch the exception in a try
/ catch block around the place where the user instantiates a client and
connects.
David

hOSAM wrote:
Why don't you just always save the exception in a variable or so inside your class that connects.. then the caller class can check for errors afterwards.
Another way you can go is that you may like to fire an event in case there is some error out there.

"David" <u0***@csc.liv.ac.uk> wrote in message
news:10****************@despina.uk.clara.net...
Hi,

Ive been trying to work this out for the past 2 days now and im not
getting anywhere fast.

The problem i have is that i am using Asynchronous sockets to create a
Socket Client library. When i try to connect to a server that doesnt
exist it raises a "Connection forcibly rejected by the resmote host"
SocketException.

Because this is thrown inside an Async Callback it is not "bubbling up"
the call stack so the user can handle it so I end up with an
UnhandledException error.

Because of the nature of the exception message i need to report this so
that the user of the client can handle this themselves (by using a
messagebox or whatever they want to do).

Does anyone know how i can cause this exception to be thrown in the
method that calls BeginInvoke() so i can bubble it back up to the user
to handle themselves in their app?

My code for this block is as follows:
=====================================

/// <summary>
/// Begins a aynchronous connection operation and delegates a async
/// callback so we don't lock up resources while waiting for the
/// operation to complete.
/// </summary>
public void Connect()
{
if(m_SocketClient != null)
{
try
{
m_SocketState = SocketState.Connecting;

// Begin the asynchronous connection to the
// host.

m_SocketClient.BeginConnect(m_ipEndPoint, new
AsyncCallback(ConnectCallBack), m_SocketClient);
}
catch(SocketException se)
{
m_SocketState = SocketState.Error;

throw new ClientException("An error has occured
while trying to connect to the remote host.",
se);
}
}
else
{
m_SocketState = SocketState.Error;

throw new ClientException("Cannot open connection when
the socket is uninitiated.");
}
}
----------------------------------------------------------------------
/// <summary>
/// Asynchronous callback for Connect method.
/// </summary>
/// <param name="asyn">Status of the asynchronous operation.</param>
private void ConnectCallBack(IAsyncResult asyn)
{
try
{
// End connection procedure.
m_SocketClient.EndConnect(asyn);

if(m_SocketClient.Connected)
{
m_SocketState = SocketState.Connected;

// Begin receiving data.
ReceiveData();

// Check to see if the event handler exists
if(OnSocketConnected != null)
{
// Create event args
ClientEventArgs args = new
ClientEventArgs(m_SocketClient);
args.SocketID = m_SocketID;
args.RemoteHost = m_HostAddress;
args.RemoteIP = m_HostIP;
args.RemotePort = m_Port;
args.State = m_SocketState;

// Raise the Connected event.
OnSocketConnected(this, args);
}
}
}
catch(SocketException se)
{
m_SocketState = SocketState.Error;

throw new ClientException("An Error occured while
attempting to end connection process with remote host.",
se);
}
}

Cheers,

David


Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Corne Oosthuizen | last post by:
I'm writing a Telnet Server application using Asynchronous sockets. I spawn a listener thread to handel incomming connections and create a separate client socket for each new connection. I...
1
by: Barry Anderberg | last post by:
I am using asynch sockets and so I call BeginReceive and then in my callback function I have certain events that would cause me to want to throw an exception. The problem of course is that the...
3
by: User N | last post by:
I'm working on a proxy which must support at least a dozen simultaneous connections from local clients to remote servers. It is conceivable that someone might want to run it in non-local mode,...
11
by: Steven | last post by:
Hi, I need to write an application using sockets. I have a server and about 10 clients "speaking" at the same time with the server, so i guess i need to use asynchronous sockets. But the server...
2
by: jasonsgeiger | last post by:
From: "Factor" <jasonsgeiger@gmail.com> Newsgroups: microsoft.public.in.csharp Subject: Multiple Clients, One port Date: Wed, 19 Apr 2006 09:36:02 -0700 I'm been working with sockets for a...
5
by: Dan Ritchie | last post by:
I've got a client/server app that I used to send large amounts of data via UDP to the client. We use it in various scenarios, one of which includes rendering a media file on the client as it is...
0
by: Raymondr | last post by:
Hi, First a brief description of out application: We have a webapplication which calls a couple of webservices during one request (postback). These calls to the webservices are made concurrent...
6
by: Richard | last post by:
Hi All, I don't know in which group my question needs to be posted so here i go: I know that the socket 'begin...' methods uses the threadpool to call the callback function but does that mean...
2
by: koredump | last post by:
Hi all, I have a windows app that makes some asyc calls to my webservice (WSE 3.0 with MTOM). exception gets thrown in the client win app. This exception is being thrown on another thread by a...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.