WinForm Network Programming (i)

Source: Internet
Author: User
Tags dns hostname

Lesson set requirements to do socket communication programming under Windows, there is no way, bad fill it. Always do not like network programming I can only slowly, to tell the truth is not dislike, is afraid of trouble.


Computer has so many layers of structure, each layer and so many protocols, understand the content and role of each protocol, the flow of communication has a clear understanding of the transmission of data understand how to parse, understand how to determine the only one specific computer network, how to resolve DNS and so on, especially the most annoying network this layer, What ip,dns,socket,tcp,http,udp wait really is silly division unclear. 、


Other than the first, can only say the network programming under C #.

1. Clear what is port ports: Think about why the computer can transmit data externally, accept data internally? Very simple an inappropriate analogy, a reservoir, can water storage and drainage, how to get in and how to come out of it, it must be through the gate of the reservoir, OK, that gate is I understand the port.

The inappropriate place is that a port can be used by different programs, but not at the same time.


2. Clear what socket sockets are: just keep in mind that the socket is between the application and the port and is used to connect the application to the ports.


3.DNS class

(1) Common methods:

Gethostaddresses returns the Internet Protocol (IP) address family for the specified host

Gethostentry resolving host names or IP addresses to Iphostentry instances

GetHostName gets the host name of the local computer and is associated with gethostbyname

Gethostbyaddress Obtaining DNS host information for an IP address

GetHostByName gets DNS information for the specified DNS hostname, which is associated with GetHostName


(2) Example code:

String hostName = Dns.gethostname ();//Gets the native name string dnsinfo = Dns.gethostbyname (Dns.gethostname ()). hostname;//to obtain DNS information based on the specified host name

4.IPAddress class

(1) Important fields, properties and methods:

The any field provides an IP address that indicates that the server should listen for client activity on all network interfaces, read-only

Address Property IP Addresses

AddressFamily the address family of a property IP address

The parse method converts an IP address string to an ipadress instance (this is common)


(2) Example code:

ipaddress[] ips = dns.gethostaddresses (This is your IP address);//Gets the IP address family of the specified host            foreach (IPAddress IP in IPs)//loop through to get the IP address            {                string ipAddress = IP. ToString ();//ipaddress is the string of the IP address            }            IPAddress IPAddress = Ipaddress.parse (This is the IP address string);


3.IPEndPoint class

Plainly, I understand that this class is equivalent to a wrapper, a combo. The connection point to the service is formed by combining the host IP address and port number of the service.

(1) Commonly used fields, properties:

Address property gets or sets the IP addresses of the endpoints

AddressFamily property gets the IP address family

The port property gets or sets the port number of the endpoint


(2) Example code:

Create IPEndPoint object, of course you can directly new IPEndPoint (IPAddress type IP address, 8888) ipendpoint ipepoint = new IPEndPoint (Ipaddress.prase ( IP address string), 8888)//gets its IP address ipAddress ipAddress = ipepoint.address//gets its port number int port = Ipepoint.port



Sum up:

summed up two ways to get a local IP address:

1.

ipaddress[] IP = dns.gethostaddresses (Dns.gethostname ()); foreach (IPAddress IP in IPs) {    IPAddress IPA = IP. Address}

Of course, this is a foreach loop, which does not write that way, so it overwrites the previous one, and this is usually the result of converting the results into a string type displayed in a label inside the foreground or inside a textbox. My side is just an example, Mo.


2.Iphostentry: The class that provides the container for the Internet host address information, you can just as a container.

Of course, you can make full use of the DNS class Gethostentry this method, the principle is similar

Iphostentry ipentry = Dns.gethostbyname (Dns.gethostname ()); IPAddress IP = ipentry.addresslist[0];


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.