This is mainly used in the following: namespace System.Net.NetworkInformation defines a class named Ipglobalproperties
Specific code
1 classPorthelper2 {3 4 #regionWhether a port of the specified type is already in use5 /// <summary>6 ///whether a port of the specified type is already in use7 /// </summary>8 /// <param name= "Port" >Port number</param>9 /// <param name= "type" >Port Type</param>Ten /// <returns></returns> One Public BOOLPortinuse (intport,porttype type) A { - BOOLFlag =false; -Ipglobalproperties Properties =ipglobalproperties.getipglobalproperties (); theipendpoint[] Ipendpoints =NULL; - if(Type = =porttype.tcp) - { -Ipendpoints =Properties. Getactivetcplisteners (); + } - Else + { AIpendpoints =Properties. Getactiveudplisteners (); at } - foreach(IPEndPoint IPEndPointinchipendpoints) - { - if(IPEndPoint. Port = =Port) - { -Flag =true; in Break; - } to } +Ipendpoints =NULL; -Properties =NULL; the returnFlag; * } $ #endregionPanax Notoginseng - } the + #regionPort Enumeration Type A /// <summary> the ///Port Type + /// </summary> - enumPortType $ { $ /// <summary> - ///TCP Type - /// </summary> the TCP, - /// <summary>Wuyi ///UDP Type the /// </summary> - UDP Wu } - #endregion
C # Determine if the port is occupied