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];