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

Resolve() public static méthode

Get IPv4 address from notation (xxx.xxx.xxx.xxx) or hostname
public static Resolve ( string ipOrHost ) : IPAddress
ipOrHost string
Résultat System.Net.IPAddress
		public static IPAddress Resolve(string ipOrHost)
		{
			if (string.IsNullOrEmpty(ipOrHost))
				throw new ArgumentException("Supplied string must not be empty", "ipOrHost");

			ipOrHost = ipOrHost.Trim();

			IPAddress ipAddress = null;
			if (IPAddress.TryParse(ipOrHost, out ipAddress))
			{
				if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
					return ipAddress;
				throw new ArgumentException("This method will not currently resolve other than ipv4 addresses");
			}

			// ok must be a host name
			IPHostEntry entry;
			try
			{
				entry = Dns.GetHostEntry(ipOrHost);
				if (entry == null)
					return null;

				// check each entry for a valid IP address
				foreach (IPAddress ipCurrent in entry.AddressList)
				{
					if (ipCurrent.AddressFamily == AddressFamily.InterNetwork)
						return ipCurrent;
				}

				return null;
			}
			catch (SocketException ex)
			{
				if (ex.SocketErrorCode == SocketError.HostNotFound)
				{
					//LogWrite(string.Format(CultureInfo.InvariantCulture, "Failed to resolve host '{0}'.", ipOrHost));
					return null;
				}
				else
				{
					throw;
				}
			}
		}

Same methods

NetUtility::Resolve ( string ipOrHost, int port ) : IPEndPoint

Usage Example

Exemple #1
0
        /// <summary>
        /// Send a message to an unconnected host
        /// </summary>
        public void SendUnconnectedMessage(NetOutgoingMessage msg, string host, int port)
        {
            if (msg == null)
            {
                throw new ArgumentNullException("msg");
            }
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }
            if (msg.m_isSent)
            {
                throw new NetException("This message has already been sent! Use NetPeer.SendMessage() to send to multiple recipients efficiently");
            }
            if (msg.LengthBytes > m_configuration.MaximumTransmissionUnit)
            {
                throw new NetException("Unconnected messages too long! Must be shorter than NetConfiguration.MaximumTransmissionUnit (currently " + m_configuration.MaximumTransmissionUnit + ")");
            }

            IPAddress adr = NetUtility.Resolve(host);

            if (adr == null)
            {
                throw new NetException("Failed to resolve " + host);
            }

            msg.m_messageType = NetMessageType.Unconnected;
            msg.m_isSent      = true;

            Interlocked.Increment(ref msg.m_recyclingCount);
            m_unsentUnconnectedMessages.Enqueue(new NetTuple <IPEndPoint, NetOutgoingMessage>(new IPEndPoint(adr, port), msg));
        }
All Usage Examples Of Lidgren.Network.NetUtility::Resolve