Advertisement
TizzyT

Better Connected TcpClient Property - TizzyT

Jul 11th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.73 KB | None | 0 0
  1. private long lastCheck = 0;
  2. public new bool Connected
  3. {
  4.     get
  5.     {
  6.         long crntCheck = Stopwatch.GetTimestamp();
  7.         if (crntCheck - lastCheck > Stopwatch.Frequency)
  8.         {
  9.             lastCheck = crntCheck;
  10.             IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
  11.             TcpConnectionInformation[] tcpConnections = ipProperties.GetActiveTcpConnections();
  12.             foreach (TcpConnectionInformation c in tcpConnections)
  13.                 if (c.LocalEndPoint.Equals(Client.LocalEndPoint) && c.RemoteEndPoint.Equals(Client.RemoteEndPoint))
  14.                     return c.State == TcpState.Established;
  15.             return false;
  16.         }
  17.         else return base.Connected;
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement