AsyncCallback exception

  • Thread starter Thread starter Steven Blair
  • Start date Start date
S

Steven Blair

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...
}
}
 
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.
 
Thanks for the reply.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top