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...
}
}
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...
}
}