What is DNS? What is IPHostEntry/IPAddress/IPEndPort? how to use?

1. What is DNS?


First look at the application of the client's partial network connection to Dns

1.IPHostEntry iPHostEntry = Dns.GetHostEntry(ipAddress);

When to use this method?
When ipAddress is a domain name, use this method to convert the domain name to ip

2.

DNSState dNSState = new DNSState();
    dNSState.hostName = address;
    dNSState.port = port;
Dns.BeginGetHostAddresses(hostNameOrAddress, GetIPAddressCallback, dNSState);

Asynchronously return the IP address of the specified host  hostNameOrAddress is the domain name

public void GetIPAddressCallback(IAsyncResult ar) {
        try {
                DNSState dnsContent = (DNSState)ar.AsyncState;
                dnsContent.resolvedIPs = Dns.EndGetHostAddresses(ar);
                _dnsInfo = dnsContent;
                Connect(_dnsInfo);
            } catch(Exception exc) {

            }

    }

3.

dnsContent.resolvedIPs = Dns.EndGetHostAddresses(ar);

Ends an asynchronous request for DNS information for saving the IP address specified by the hostNameOrAddress parameter of BeginGetHostAddresses

From the above three examples, it can be seen that the function of Dns is to resolve the domain name into an IP address that the host can recognize

PS:

What is a domain name? WWW.Baidu.com is the domain name;

                      192.168.0.1 is the IP address


2. What is IPHostEntry/IPAddress/IPEndPort?

IPHostEntry :

Is a container class that associates Domain Name System (Dns) hostnames and aliases (IPHostEntry.Aliases) with matching IP addresses. It provides the host's ip address (with the help of the IPAddress class)

eg:

IPHostEntry iPHostEntry = Dns.GetHostEntry(ipAddress);

Dns (Domain Name System) converts the domain name ipAddress to iPHostEntry through the GetHostEntry method . iPHostEntry.AddressList[0] is the IP address corresponding to ipAddress


IPAddress:

The IP address recognized by the host

1. Convert the ip address of the string type into a parameter of the IPAddress type

IPAddress.TryParse(ipAddress, out address)

ipAddress      string type parameter
address          IPAddress type parameter


IPEndPort:

Is the IP address + port number

IPEndPort iPEndPoint = new IPEndPoint(IPAddress address,int port);

eg:

_socket.BeginConnect(iPEndPoint, ConnetSuccess, _socket);

Guess you like

Origin blog.csdn.net/SmillCool/article/details/128544414