D
David
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
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