Constructing web downloader in .NET environment (1)

xiaoxiao2021-03-06  39

Web page download is under construction .NET environment (1) Author: Wang Kaiming www.ASPCool.com Time: 2003-8-29 13:20:36 Views: 5821

One. Preface: Microsoft's .NET Framework provides us with two namespaces for us to provide the following namespace: System.Net and System.Net.Sockets. By reasonable use of the classes and methods, we can easily write a variety of web applications. This network application can either generate a stream sleeve, or may be based on a data sheet. And the most widely used protocol based on the communication-based communication is the TCP protocol, and the most widely natural use of the data report-based communications is the UDP protocol. Below I will focus on some classes in .NET network programming: DNS class, iPhostentry, IpendPoint, and socket classes, and will give an instance of a web downloader to deepen readers to .NET network programming . Two. Network Programming Class Introduction: DNS Class: Provides domain name services to applications that use TCP / IP Internet services. Its Resolve () method queries the DNS server to map the user-friendly domain name (such as "www.google.com") to the Internet address (e.g., 192.168.1.1). The resolve () method returns an iPhostenty instance that contains the address of the requested name and alias. In most cases, you can use the first address returned in the AddressList array.

The result of the resolve () method is as follows: public static iphostentry resolve (String hostname); The following code gets an ipaddress instance, which contains the IP address of the server www.google.com: iPhostentry iphostinfo = dns.resolve ("www.google .com "); ipaddress ipaddress = iphostinfo.addresslist [0]; however, in the DNS class, in addition to the Resolve () method, you can get the corresponding iPhostRy instance through the gethostbyaddress () method, the gethostByname () method, function prototype as follows: public static IPHostEntry GetHostByAddress (string IPAddress); public static IPHostEntry GetHostByName (string hostName); the following code shows how IPHostEntry example use of the above two methods, respectively, contain information about the server www.google.com: IPHostEntry hostInfo = DNS.GethOSTBYADDRESS ("192.168.1.1"); iPhostentry Hostinfo = DNS.GETOSTBYNAME ("www.google.com"); When using the above method, you will have to handle the following exceptions: Socketexception is exception: Operation when accessing socket The system error trigger argumentnullexception: Parameter is an empty reference to trigger ObjectDisposedexception exception: Socket has been turned off, I briefly introduced some methods in the DNS class and its usage, and raise the abnormality that may appear, let us Go to the IPHOSTENTRY class that is closely related to the DNS class. IPHOSTENTRY Class: The instance object of this class contains address information about the Internet host. All public static members of this type are secure for multi-threaded operations, but not to ensure that any instance members are threads. Some of these properties are: addressList properties, aliases properties, and hostname properties. The use of AddressList properties and aliases properties is the list of IP addresses associated with the host, respectively, and gain or set the alias list associated with the host. Where the addressList property is an array of ipaddress types, which contains the IP address of the host name contained in the AliaSES property; the AliaSs property value is a set of strings, which contains the DNS name of the IP address resolved into the AddressList property. The HostName property is better understood that it contains the main host name of the server. This light can know from the name. If the server's DNS item defines an additional alias, you can use these alias in the AliaSes property.

The following code lists the relevant alias lists of the server www.google.com and the length of the IP address list and lists all IP addresses: iphostentry iphost = dns.resolve ("www.google.com/"); string [ ] aliases = iphost.aliases; console.writeline (aliases.length); ipaddress [] addr = iphost.addresslist; console.writeline (addr.Length); for (int i = 0; i

In asynchronous mode, these calls return immediately. Let's focus on the Socket programming of the synchronous mode. First, the basic procedure of the Socket programming of the synchronous mode is as follows: 1. Create a Socket instance object. 2. Connect the above instance object to a specific endpoint (endpoint). 3. After the connection is completed, you can communicate with the server: receive and send information. 4. Communication is completed, use the shutdown () method to disable the socket. 5. Finally, use a Close () method to close the socket. I know the above basic procedure, we have begun to further achieve and communicate. Before use, you need to create Socket object instance, which may be achieved by the Socket class constructor: public Socket (AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType); wherein, AddressFamily Socket parameter specifies the addressing scheme used, such as AddressFamily.InterNetwork indicates the address of the IP version 4; the SocketType parameter specifies the type of socket, such as SocketType.Stream indicates that the connection is based on a streamlined, and the sockettype.dgram represents the connection based on the data setup. The protocoltype parameter specifies the protocol used by the socket, such as ProtocolType.TCP indicates that the connection protocol is used by TCP protocol, and protocol.udp indicates that the connection protocol is used to use UDP protocol. After the Socket instance can be created, we can pass the endpoint of a remote host and it acquire the connection. The method used is the connection () method: public connect (EndPoint EP); this method can only be used on the client. After the connection, we can use the connect of the connectivity to verify that the connection is successful. If the returned value is True, the connection is successful, otherwise it is failed. The following code shows how to create a socket instance and get a connection through the endpoint and the process: iphostentry iphost = dns.resolve ("http://www.google.com/"); string [] aliases = iphost.aliases ; IPAddress [] addr = IPHost.AddressList; EndPoint ep = new IPEndPoint (addr [0], 80); Socket sock = newSocket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sock.Connect (ep); if (SOCK.CONNECTED) Console.WriteLine ("OK"); once the connection is successful, we can use the send () and request () methods to communicate.

The function prototype of the Send () method is as follows: Public int send (Byte [] Buffer, int size, where the parameter buffer contains the data to be sent, the parameter size indicates the size of the data to send, and the parameter Flags can Some values: socketflags.none, socketflags.dontroute, socketflags.outofbnd. This method returns a value of a System.Int32 type that indicates the size of the transmitted data. At the same time, the method has several functions that have been overloaded: public int send (byte [] buffer; public int send (byte [] buffer, socketflags flags; public int desund (byte [] buffer, INT Offset, int size, socketflags flags; introduced the send () method, the following is the Receive () method, its function prototype as follows: Public int received (byte [] buffer, int size, socketflags flags; parameters and Send () The parameters of the method are similar, and will not be described here. Similarly, the method has some of the following functions have been reached: public int received; public int received (byte [] buffer, socketflags flags); public int received (Byte [] buffer, int Offset, int size, socketflags flags; after the communication is complete, we disable the socket through the shutdown () method, the function prototype is as follows: Public void shutdown (socketshutdown how); the parameter how to disable the disabled type, SoketshutDown.send Indicates that the socket used to send; SoketShutdown.Receive indicates that the socket used to receive; and SoketShutdown.both indicates that the send and reception socket are simultaneously closed. It should be noted that the shutdown () method must be invoked before calling the Close () method to ensure that all pending data is sent or received before the Socket is turned off. Once the shutdown () is called, the Close () method is called to close the socket, the prototype is as follows: public void close (); this method is forced to close a socket connection and release all managed resources and unmanaged resources.

转载请注明原文地址:https://www.9cbs.com/read-72602.html

New Post(0)