D
Dinsdale
I am trying to write a Tcp "Server" that opens a class that wraps a tcp
socket when a new connection is made (Listener.AcceptSocket()).
Everything is going swimmingly except when I try to close the socket
during a read and I get the following error:
<error_msg>
An unhandled exception of type 'System.Net.Sockets.SocketException'
occurred in system.dll
Additional information: The I/O operation has been aborted because of
either a thread exit or an application request
</error_msg>
The following is the delegate function for the BeginReceive call:
private void ReadStream(IAsyncResult ar)
{
if(IsAvailable)
{
lock(_socClient) //*** - Futile attempt to stop the exception
{
int intByteCount = _socClient.EndReceive(ar);//**** - THIS
IS WHERE I GET MY EXCEPTION
if(intByteCount > 0)
{
_tmrTimeout.Stop();
ProcessInput(_abytBuffer, intByteCount);//Adds any
ASCII text to a string buffer
if(IsAvailable)
{
_socClient.BeginReceive(_abytBuffer,0,
BUFFER_LENGTH,SocketFlags.None,
_acbBeginReadCallback,_socClient);
}
_tmrTimeout.Start();
}
else
{
Close(true);
}
}
}
else
{
Close(false);
}
}
My shut down routine looks like this:
public bool IsAvailable
{
get
{
if((!_bShutdown) && (_socClient != null) &&
_socClient.Connected)
{
return true;
}
else
{
return false;
}
}
}
public void Close(bool bSendStatusUpdate)
{
if(IsAvailable)
{
lock(_socClient)
{
_bShutdown = true; //An attempt to indicate what is going
on... SO didn't work
_socClient.Shutdown(SocketShutdown.Both);
_socClient.Close();
}
//_socClient = null;
}
_tmrTimeout.Stop();
if(bSendStatusUpdate)
{
OnClientStatusChanged(new ClientStatusChangedEventArgs(
CommStatusType.CLOSED,RemoteEndPoint,"Closed"));
}
}
#region IDisposable Members
public void Dispose()
{
if(IsAvailable)
{
Close(false);
}
}
#endregion
I thought that maybe I can manually call the EndReceive function from
the Close() routine but I cannot declare a IAsyncResult object. Any
suggestions to close the connection would be great.
Once again, thank you to the Google community for saving my behind.
I'll try to answer a couple of other messages in return.
Cheers,
Russ
socket when a new connection is made (Listener.AcceptSocket()).
Everything is going swimmingly except when I try to close the socket
during a read and I get the following error:
<error_msg>
An unhandled exception of type 'System.Net.Sockets.SocketException'
occurred in system.dll
Additional information: The I/O operation has been aborted because of
either a thread exit or an application request
</error_msg>
The following is the delegate function for the BeginReceive call:
private void ReadStream(IAsyncResult ar)
{
if(IsAvailable)
{
lock(_socClient) //*** - Futile attempt to stop the exception
{
int intByteCount = _socClient.EndReceive(ar);//**** - THIS
IS WHERE I GET MY EXCEPTION
if(intByteCount > 0)
{
_tmrTimeout.Stop();
ProcessInput(_abytBuffer, intByteCount);//Adds any
ASCII text to a string buffer
if(IsAvailable)
{
_socClient.BeginReceive(_abytBuffer,0,
BUFFER_LENGTH,SocketFlags.None,
_acbBeginReadCallback,_socClient);
}
_tmrTimeout.Start();
}
else
{
Close(true);
}
}
}
else
{
Close(false);
}
}
My shut down routine looks like this:
public bool IsAvailable
{
get
{
if((!_bShutdown) && (_socClient != null) &&
_socClient.Connected)
{
return true;
}
else
{
return false;
}
}
}
public void Close(bool bSendStatusUpdate)
{
if(IsAvailable)
{
lock(_socClient)
{
_bShutdown = true; //An attempt to indicate what is going
on... SO didn't work
_socClient.Shutdown(SocketShutdown.Both);
_socClient.Close();
}
//_socClient = null;
}
_tmrTimeout.Stop();
if(bSendStatusUpdate)
{
OnClientStatusChanged(new ClientStatusChangedEventArgs(
CommStatusType.CLOSED,RemoteEndPoint,"Closed"));
}
}
#region IDisposable Members
public void Dispose()
{
if(IsAvailable)
{
Close(false);
}
}
#endregion
I thought that maybe I can manually call the EndReceive function from
the Close() routine but I cannot declare a IAsyncResult object. Any
suggestions to close the connection would be great.
Once again, thank you to the Google community for saving my behind.
I'll try to answer a couple of other messages in return.
Cheers,
Russ