Sunday, April 17, 2011

Best way to check if a TCP connection is active

Given a list of IP addresses how do I programmatically check if the local machine still has active TCP connections to these IP Addresses? I am using C#.

From stackoverflow
  • Call GetExtendedTcpTable and check through the list for your target addresses

  • using System.Net.NetworkInformation    
    
    IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
    TcpConnectionInformation[] tcpInfoList = properties.GetActiveTcpConnections();
    
    Noldorin : Good solution... I wasn't actually aware there was a simple managed call to get such information.
  • I do not understand question exactly, but in case if you have list of addresses with a software that listen on some ports, try to connect there using eg. Socket class:

    Socket m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    m_Socket.Connect(serverEndPoint);
    

    and eventually try to catch an exception...

    If you have connection that are already made in you code you can check m_Socket.Connected property..

0 comments:

Post a Comment