Build a web downloader in the Socket class Sunhai
Development Tools: Microsoft Visual Studio .NET 2003
Operating system: Windows XP
When can I use the Socket class Microsoft .NET Framework to access the Internet to provide hierarchical, scalable and jurisded web services, their namespace System.Net and System.Net.Sockets include rich classes to develop more Network application. The so-called "layered" can be considered to include 3 levels: request / response layer, application protocol layer, transport layer. WebReQEUST and WebResponse represent the request / response layer, which supports HTTP, TCP, and UDP classes that form the application protocol layer, while the Socket class is at the bottom-transport layer. "Links in HTTPWebRequest and Regular Expression Extracts" describes the web source code with the request / response layer. This article uses the most underlying transport layer to obtain web source code. Why use Socket? Or when you want to use the Socket? Socket class to provide a wide range of methods and properties for network communications. The Socket class allows you to perform asynchronous and synchronous data transfer using any of the protocols listed below:
GGP gateway to the gateway protocol. ICMP Internet Message Control Protocol. IDPIDP protocol. IGMP Internet Management Agreement. IP Internet Agreement. IPv6 Internet Protocol V6. IPXIPX protocol. ND Network Disk Protocol (informal). Puppup protocol. Raw Original UP Package Protocol. SPXSPX protocol. SPXISPX version 2 protocol. TCP Transmission Control Protocol. UDP User Data Network Protocol. Unknown unknown agreement. Unspecified unspecified protocol.
When you need to develop a complex network program, you may have to use the Socket class. Get the web source code first introduced the following namespace:
Imports system.netimports system.net.socketsimports system.iimports system.textimports system.text.encoding
I originally used C # to practice this text code, C # strictly sink, and the function of intelligent awareness is obviously not as VB.NET, although the code is simple, but it is not adaptable. I think, Microsoft really pushes may be VB.NET, otherwise why not implement the same intelligent perception like VB.NET in C #? Finally, use VB.NET to write this document. The initiator may be a bit awkward, not tight, first start from a simple instance, instance debugging success, and then understand the theoretical knowledge is not too late.
Private Sub btnHTML_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHTML.Click Dim serverIP As IPAddress = Dns.Resolve ( "http://sunhai.tianyablog.com") .AddressList (0) 'Default Web Server Port = 80 Dim Port As String = "80" Dim serverhost As New IPEndPoint (serverIP, Int32.Parse (Port)) Dim clientSocket As New Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Try clientSocket.Connect (serverhost) If clientSocket.Connected = False Then MsgBox ( "Connect Error.", MsgBoxStyle.Critical, "HTTP") Exit Sub End If Dim httpReq As String = "GET / HTTP / 1.0" & ControlChars.CrLf & ControlChars.CrLf clientSocket.Send (ASCII.GetBytes (httpReq)) Dim buffer (1024) As Byte Dim byteCount As Int16 = clientSocket.Receive (buffer, buffer.Length, 0) txtHTML.Text = ASCII.GetString (buffer, 0, byteCount) Do While Bytecount> 0 Bytecount = Clientsocket.Receive (Buffer, Buffer.Length, 0) TXTHTML.TEXT = TXTHTML.TEX T & ascii.getstring (buffer, 0, bytecount) loop catch exception msgbox (ex.stacktrace.tostring (), msgboxstyle.critical, "exception") End Trynd Sub Contact me: My QQ: 26624998 My Website : Http://sunhai.tianyablog.com This article address: http://www.9cbs.net/develop/read_article.asp? Id = 25281
Network Resources: Windows Socket Network Programming - Sackage Programming Principle Visual C # Hosting Socket Implementation C # Network Programming A Probe into Visual C # .NET Network Program Development - Socket Typography C # Sockets in C #
March 5, 2004
My QQ: 26624998 My website: http://blog.9cbs.net/2066/