/// <summary> 检测端口是否占用 </summary> /// <param name="port"> </param> /// <returns> </returns> public static bool PortInUse(int port) { bool inUse = false; System.Net.NetworkInformation.IPGlobalProperties ipProperties = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties(); IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners(); foreach (IPEndPoint endPoint in ipEndPoints) { if (endPoint.Port == port) { inUse = true; break; } } return(inUse); }
/// <summary> /// 判断当前绑定的ip地址是否已经被绑定 /// </summary> /// <param name="port"></param> /// <returns></returns> internal static Boolean IsPortOccupedFun2(Int32 port) { Boolean result = false; try { System.Net.NetworkInformation.IPGlobalProperties iproperties = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties(); System.Net.IPEndPoint[] ipEndPoints = iproperties.GetActiveTcpListeners(); foreach (var item in ipEndPoints) { if (item.Port == port) { result = true; break; } } } catch (Exception ex) { throw ex; } return(result); }
private string GetTcpStatistics( NetworkInterfaceComponent version, IPGlobalProperties properties) { TcpStatistics tcpStat = null; StringBuilder tcpStatBuilder = new StringBuilder(); try { switch (version) { case NetworkInterfaceComponent.IPv4: tcpStat = properties.GetTcpIPv4Statistics(); tcpStatBuilder.Append("TCP/IPv4 Statistics\n"); break; case NetworkInterfaceComponent.IPv6: tcpStat = properties.GetTcpIPv6Statistics(); tcpStatBuilder.Append("TCP/IPv6 Statistics\n"); break; default: throw new ArgumentException("No such IP version"); } } catch (NetworkInformationException) { return String.Empty; } tcpStatBuilder.Append("=================================================\n"); tcpStatBuilder.AppendFormat(" Minimum Transmission Timeout .. : {0}\n", tcpStat.MinimumTransmissionTimeout); tcpStatBuilder.AppendFormat(" Maximum Transmission Timeout .. : {0}\n", tcpStat.MaximumTransmissionTimeout); tcpStatBuilder.AppendFormat(" Maximum Connections ........... : {0}\n", tcpStat.MaximumConnections); tcpStatBuilder.AppendFormat(" Connection Data:\n"); tcpStatBuilder.AppendFormat(" Current ...................... : {0}\n", tcpStat.CurrentConnections); tcpStatBuilder.AppendFormat(" Cumulative ................... : {0}\n", tcpStat.CumulativeConnections); tcpStatBuilder.AppendFormat(" Initiated .................... : {0}\n", tcpStat.ConnectionsInitiated); tcpStatBuilder.AppendFormat(" Accepted ..................... : {0}\n", tcpStat.ConnectionsAccepted); tcpStatBuilder.AppendFormat(" Failed Attempts .............. : {0}\n", tcpStat.FailedConnectionAttempts); tcpStatBuilder.AppendFormat(" Reset ........................ : {0}\n", tcpStat.ResetConnections); tcpStatBuilder.AppendFormat(" Errors ....................... : {0}\n", tcpStat.ErrorsReceived); tcpStatBuilder.AppendFormat(" Segment Data:\n"); tcpStatBuilder.AppendFormat(" Received ..................... : {0}\n", tcpStat.SegmentsReceived); tcpStatBuilder.AppendFormat(" Sent ......................... : {0}\n", tcpStat.SegmentsSent); tcpStatBuilder.AppendFormat(" Retransmitted ................ : {0}\n", tcpStat.SegmentsResent); tcpStatBuilder.AppendFormat(" Resent with reset ............ : {0}\n", tcpStat.ResetsSent); TcpConnectionInformation[] connectionsInfo = properties.GetActiveTcpConnections(); tcpStatBuilder.AppendFormat(" Active Connections ............ : {0}\n", connectionsInfo.Length); if (connectionsInfo.Length > 0) { tcpStatBuilder.Append(" Local Address Port Remote Address Port\n"); } foreach (TcpConnectionInformation connectionInfo in connectionsInfo) { tcpStatBuilder.AppendFormat(" {0, -15}:{1} {2, -15}:{3}\n", connectionInfo.LocalEndPoint.Address, connectionInfo.LocalEndPoint.Port, connectionInfo.RemoteEndPoint.Address, connectionInfo.RemoteEndPoint.Port); } IPEndPoint[] endPoints = properties.GetActiveTcpListeners(); tcpStatBuilder.AppendFormat(" TCP Listeners ................. : {0}\n", endPoints.Length); if (endPoints.Length > 0) { tcpStatBuilder.Append(" Local Address Port\n"); } foreach (IPEndPoint endPoint in endPoints) { tcpStatBuilder.AppendFormat(" {0, -15}:{1}\n", endPoint.Address, endPoint.Port); } return tcpStatBuilder.ToString(); }
private void timer1_Tick(object sender, EventArgs e) { System.Net.NetworkInformation.IPGlobalProperties ipgp = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties(); System.Net.NetworkInformation.TcpStatistics ts = ipgp.GetTcpIPv4Statistics(); System.Net.NetworkInformation.UdpStatistics us = ipgp.GetUdpIPv4Statistics(); ArrayList al = new ArrayList(); ArrayList alus = new ArrayList(); al.Add(ts); alus.Add(us); if (dataGridView6.DataSource == null || (dataGridView6.DataSource != null && !((ArrayList)dataGridView6.DataSource).Equals(alus))) { try { dataGridView6.DataSource = alus; } catch { } } if (dataGridView5.DataSource == null || (dataGridView5.DataSource != null && !((ArrayList)dataGridView5.DataSource).Equals(al))) { try { dataGridView5.DataSource = al; } catch { } } System.Net.IPEndPoint[] udplist = ipgp.GetActiveUdpListeners(); if (dataGridView4.DataSource == null || (dataGridView4.DataSource != null && !((System.Net.IPEndPoint[])dataGridView4.DataSource).Equals(udplist))) { try { dataGridView4.DataSource = udplist; } catch { } } System.Net.IPEndPoint[] tcplist = ipgp.GetActiveTcpListeners(); if (dataGridView3.DataSource == null || (dataGridView3.DataSource != null && !((System.Net.IPEndPoint[])dataGridView3.DataSource).Equals(tcplist))) { try { dataGridView3.DataSource = tcplist; } catch { } } System.Net.NetworkInformation.TcpConnectionInformation[] tci = ipgp.GetActiveTcpConnections(); if (dataGridView2.DataSource == null || (dataGridView2.DataSource != null && !((System.Net.NetworkInformation.TcpConnectionInformation[])dataGridView2.DataSource).Equals(tci))) { try { dataGridView2.DataSource = tci; } catch { } } FileInfo nfi = new FileInfo(strPath); if (nfi.LastWriteTime > fi.LastWriteTime) { RefreshLog(); } }