TcpListener.AcceptTcpClient OR TimeOut
-
It seems that I'm stuck on something small but I've been working on it for quite some time and I'm exhausted... I have a method that returns a bool whether a TcpConnection was successful or not... The method attempts TcpListener.AcceptTcpClient however I want it to return false if no client has been connected within a specified time (e.g. 2000 milliseconds)... How can I achieve this? I attempted the .BeginAcceptTcpClient and .EndAcceptTcpClient but these still block OR if I use the Thread.Sleep() I block the method from accepting any connecting client.. I need the result to be returned if a TcpClient is accepted or the operation 'timed-out' - whichever comes first. Please help!
-
It seems that I'm stuck on something small but I've been working on it for quite some time and I'm exhausted... I have a method that returns a bool whether a TcpConnection was successful or not... The method attempts TcpListener.AcceptTcpClient however I want it to return false if no client has been connected within a specified time (e.g. 2000 milliseconds)... How can I achieve this? I attempted the .BeginAcceptTcpClient and .EndAcceptTcpClient but these still block OR if I use the Thread.Sleep() I block the method from accepting any connecting client.. I need the result to be returned if a TcpClient is accepted or the operation 'timed-out' - whichever comes first. Please help!
something like this?
[pseudo C#]
private bool GetNext(out TcpClient client)
{
client = null;while ((less than 2000 ms has elapsed) and (!TcpListener.Pending()) { Sleep a bit } if (TcpListener.Pending()) { client = TcpListener.AcceptTcpClient(); return true; } return false;
}
:badger: