通过一个小程序,说明IIS不神秘
构想,这个只是构想而已:自己做一个IIS。
IIS是什么呢?一个霸占者80或者443端口的程序。
所有对于80端口的请求,不管是GET还是POST或者其他符合HTTP1.1的东西,都交给它来处理了。
通过浏览器,我们请求一个页面,然后,我们的请求就被它获得了。我们看看,它拿到的HTTP请求数据头是什么东西。
新建一个VBNET的控制台程序,我们也来霸占一个端口 13000
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic
Module Module1
Public Sub Main()
Dim server As TcpListener
server = Nothing
Try
' Set the TcpListener on port 13000.
Dim port As Int32 = 13000
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
server = New TcpListener(localAddr, port)
' Start listening for client requests.
server.Start()
' Buffer for reading data
Dim bytes(1024) As Byte
Dim data As String = Nothing
' Enter the listening loop.
While True
Console.Write("Waiting for a connection... ")
' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
Dim client As TcpClient = server.AcceptTcpClient()
Console.WriteLine("Connected!")
data = Nothing
' Get a stream object for reading and writing
Dim stream As NetworkStream = client.GetStream()
Dim i As Int32
' Loop to receive all the data sent by the client.
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
' Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Console.WriteLine("Received: {0}", data)
' Process the data sent by the client.
data = data.ToUpper()
Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
' Send back a response.
stream.Write(msg, 0, msg.Length)
Console.WriteLine("Sent: {0}", data)
i = stream.Read(bytes, 0, bytes.Length)
End While
' Shutdown and end connection
client.Close()
End While
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
Finally
server.Stop()
End Try
Console.WriteLine(ControlChars.Cr + "Hit enter to continue....")
Console.Read()
End Sub 'Main
End Module
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic
Module Module1
Public Sub Main()
Dim server As TcpListener
server = Nothing
Try
' Set the TcpListener on port 13000.
Dim port As Int32 = 13000
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
server = New TcpListener(localAddr, port)
' Start listening for client requests.
server.Start()
' Buffer for reading data
Dim bytes(1024) As Byte
Dim data As String = Nothing
' Enter the listening loop.
While True
Console.Write("Waiting for a connection... ")
' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
Dim client As TcpClient = server.AcceptTcpClient()
Console.WriteLine("Connected!")
data = Nothing
' Get a stream object for reading and writing
Dim stream As NetworkStream = client.GetStream()
Dim i As Int32
' Loop to receive all the data sent by the client.
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
' Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Console.WriteLine("Received: {0}", data)
' Process the data sent by the client.
data = data.ToUpper()
Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
' Send back a response.
stream.Write(msg, 0, msg.Length)
Console.WriteLine("Sent: {0}", data)
i = stream.Read(bytes, 0, bytes.Length)
End While
' Shutdown and end connection
client.Close()
End While
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
Finally
server.Stop()
End Try
Console.WriteLine(ControlChars.Cr + "Hit enter to continue....")
Console.Read()
End Sub 'Main
End Module
启动这个程序,然后找个浏览器,在地址栏输入
http://localhost:13000/
你的浏览器什么现在都没有显示【过一会改一下代码就会好的,别急】。看看控制台输出了什么。前面一半是截获到的浏览器发出的请求。后面一半是我们返回给浏览器的东西【就是把请求转为大写,当然浏览器不知道怎么办了】。这个请求的HTTP数据头,网上资料一大把。
OK,现在我们修改一下程序,还给浏览器一些它想要的东西,就是HTTP返回的数据头。
1 ' Process the data sent by the client.
2 data = "HTTP/1.1 200 OK" & vbCrLf
3 data += "Content-Length: 37" & vbCrLf & vbCrLf
4 data += "<HTML><BODY>Hello World</BODY></HTML>"
2 data = "HTTP/1.1 200 OK" & vbCrLf
3 data += "Content-Length: 37" & vbCrLf & vbCrLf
4 data += "<HTML><BODY>Hello World</BODY></HTML>"
第二行,我们返回200,表示请求处理成功
第三行,我们告诉浏览器正式数据的长度,并且加入一个空行,表示数据头和数据的区分。
第四行,就是我们的HTML了。
重复运行一下程序:再浏览器里面输入
http://localhost:13000/
现在就有HelloWorld了吧。
其实IIS干的事情就和这个小程序差不多。霸占80端口,等着请求(Request,GET/POST),请求来了,解析一下,找个合适的同志来处理请求(ASPNET),处理好的请求的结果(response),还给浏览器。
各位看了这个例子之后,就知道IIS做的事情,也不是很神秘吧。
实际上IIS还要管理Session(可能通过Cookies来实现)调度任务 等等。不过机理还是围绕着HTTP1.1协议来展开的。希望大家知道IIS的大概工作原理,不要觉得IIS是一个小黑屋。输入一个网址到浏览器,获得一个HTML的页面,其实很简单。