Lidgren.Network.NetUtility.GetMyAddress C# (CSharp) Méthode

GetMyAddress() public static méthode

Gets my local IP address (not necessarily external) and subnet mask
public static GetMyAddress ( IPAddress &mask ) : IPAddress
mask System.Net.IPAddress
Résultat System.Net.IPAddress
		public static IPAddress GetMyAddress(out IPAddress mask)
		{
#if IS_FULL_NET_AVAILABLE
			NetworkInterface ni = GetNetworkInterface();
			if (ni == null)
			{
				mask = null;
				return null;
			}

			IPInterfaceProperties properties = ni.GetIPProperties();
			foreach (UnicastIPAddressInformation unicastAddress in properties.UnicastAddresses)
			{
				if (unicastAddress != null && unicastAddress.Address != null && unicastAddress.Address.AddressFamily == AddressFamily.InterNetwork)
				{
					mask = unicastAddress.IPv4Mask;
					return unicastAddress.Address;
				}
			}
#endif
			mask = null;
			return null;
		}

Usage Example

Exemple #1
0
        /// <summary>
        /// Add a forwarding rule to the router using UPnP
        /// </summary>
        public bool ForwardPort(int port, string description)
        {
            if (m_serviceUrl == null)
            {
                return(false);
            }

            IPAddress mask;
            var       client = NetUtility.GetMyAddress(out mask);

            try
            {
                XmlDocument xdoc = SOAPRequest(m_serviceUrl,
                                               "<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
                                               "<NewRemoteHost></NewRemoteHost><NewExternalPort>" + port.ToString() + "</NewExternalPort>" +
                                               "<NewProtocol>" + ProtocolType.Udp.ToString().ToUpper() + "</NewProtocol>" +
                                               "<NewInternalPort>" + port.ToString() + "</NewInternalPort>" +
                                               "<NewInternalClient>" + client.ToString() + "</NewInternalClient>" +
                                               "<NewEnabled>1</NewEnabled>" +
                                               "<NewPortMappingDescription>" + description + "</NewPortMappingDescription>" +
                                               "<NewLeaseDuration>0</NewLeaseDuration>" +
                                               "</u:AddPortMapping>",
                                               "AddPortMapping");

                m_peer.LogDebug("Sent UPnP port forward request");
                System.Threading.Thread.Sleep(50);
            }
            catch (Exception ex)
            {
                m_peer.LogWarning("UPnP port forward failed: " + ex.Message);
                return(false);
            }
            return(true);
        }
All Usage Examples Of Lidgren.Network.NetUtility::GetMyAddress