PC Review


Reply
Thread Tools Rate Thread

AsyncCallback exception

 
 
Steven Blair
Guest
Posts: n/a
 
      16th Feb 2007
Hi,

I am using an AsyncCallback to receive a TcpClient on incoming
connections. The code below works fine until I start the application and
no connection is received within the time limit.
I could easily try / catch the code causing the problem but I would like
to understand why this problem is happening.
I was unable to find any field on the TcpListener that would indicate
the Listenign socket was stopped.


http://pastebin.com/882324 or

static private TcpClient _connectedClient;

static void AcceptClient(IAsyncResult ar)
{
TcpListener server = (TcpListener)ar.AsyncState;

_connectedClient = _listenSocket.EndAcceptTcpClient(ar);
}

static void Main()
{
TcpListener _listenSocket = new TcpListener(8005);
_listenSocket.Start();

_listenSocket.BeginAcceptTcpClient(new AsyncCallback(AcceptClient),
_listenSocket);

//Wait for 5 seconds for a connection
Thread.Sleep(5000);

if( _connectedClient == null )
{
//Failed to get a connection
//This call to stop invokes a call to AcceptClient
//This cause AcceptClient to crash on the EndAcceptTcpClient
_listenSocket.Stop();
}
else
{
//Application begins...
}
}



*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
 
Laura T.
Guest
Posts: n/a
 
      16th Feb 2007
If you get SocketException it is because the socket is closed and the async
request is cancelled and it shows up as an exception at EndAcceptTcpClient.

"Steven Blair" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed)...
> Hi,
>
> I am using an AsyncCallback to receive a TcpClient on incoming
> connections. The code below works fine until I start the application and
> no connection is received within the time limit.
> I could easily try / catch the code causing the problem but I would like
> to understand why this problem is happening.
> I was unable to find any field on the TcpListener that would indicate
> the Listenign socket was stopped.
>
>
> http://pastebin.com/882324 or
>
> static private TcpClient _connectedClient;
>
> static void AcceptClient(IAsyncResult ar)
> {
> TcpListener server = (TcpListener)ar.AsyncState;
>
> _connectedClient = _listenSocket.EndAcceptTcpClient(ar);
> }
>
> static void Main()
> {
> TcpListener _listenSocket = new TcpListener(8005);
> _listenSocket.Start();
>
> _listenSocket.BeginAcceptTcpClient(new AsyncCallback(AcceptClient),
> _listenSocket);
>
> //Wait for 5 seconds for a connection
> Thread.Sleep(5000);
>
> if( _connectedClient == null )
> {
> //Failed to get a connection
> //This call to stop invokes a call to AcceptClient
> //This cause AcceptClient to crash on the EndAcceptTcpClient
> _listenSocket.Stop();
> }
> else
> {
> //Application begins...
> }
> }
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***



 
Reply With Quote
 
Steven Blair
Guest
Posts: n/a
 
      16th Feb 2007
Thanks for the reply.

Yeah, thats what I thought.
Any idea how I can check to see the Socket is closed?



*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AsyncCallback called twice on Exception Craig Vermeer Microsoft Dot NET 2 11th Nov 2005 08:22 PM
unhandled exception not being caught in asyncCallback's method TS Microsoft Dot NET Framework Forms 1 13th Jan 2005 06:54 AM
Exception with AsyncCallBack + Control.Invoke Matt Microsoft Dot NET Framework Forms 0 13th Jul 2004 08:13 PM
Exception with AsyncCallBack + Control.Invoke Matt Microsoft Dot NET Framework 0 13th Jul 2004 08:07 PM
AsyncCallback + Exception Matt Stone Microsoft Dot NET Framework Forms 0 8th Jan 2004 04:28 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:38 PM.