简体   繁体   中英

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

using System.Net.NetworkInformation    

IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] tcpInfoList = properties.GetActiveTcpConnections();

Call GetExtendedTcpTable and check through the list for your target addresses

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM