使用VB建立Web Server一例
来源:岁月联盟
时间:2007-02-01
一、建立窗体
控件依次为label1,label2,label3,
text1,text2,text3,command1,及Winsock1
label1.caption=”Visual Basic Web 服务器 1.0”
label2.caption=”本机地址”
label3.caption=”发布路径”
text2.text=”c:/Inetpub/wwwroot/”
command1.caption=”启动”
又击Command1加入代码
Private Sub Command1_Click()
'启动Winsock1,使用listen方法,听80端口
DoEvents
Winsock1.Close
Winsock1.Protocol = sckTCPProtocol
Winsock1.LocalPort = 80
Winsock1.Listen
Text1.Text = "WinSocket状态:
" & CStr(Winsock1.State) + "
本机IP地址:" + CStr(Winsock1.LocalIP)
End Sub
然后在Winsock1 的DataArrival事件中加入代码
Winsock1.GetData DataReceived, vbString
Text3.Text = DataReceived
pos1 = 0
pos2 = 0
For i = 1 To Len(DataReceived)
If Mid(DataReceived, i, 1) = " " Then
If pos1 = 0 Then
pos1 = i + 1
Else
pos2 = i - 1
Exit For
End If
End If
Next
On Error GoTo ExitThisSub
If pos1 < > 0 And pos2 < > 0 Then
requestfilename = Mid(DataReceived, pos1 + 1, pos2 - pos1)
requestfilename = Trim(Text2.Text) & requestfilename
If Dir(requestfilename) < > "" Then
Else
GoTo NoRequestFile
End If
End If
Open requestfilename For Input As #1
Winsock1.SendData "HTTP/1.0 200 OK" + vbCrLf
Winsock1.SendData "MIME_version:1.0" + vbCrLf
Winsock1.SendData "Content_Type:text/html" + vbCrLf
Winsock1.SendData
"Content_Length:" + CStr(LOF(1)) + vbCrLf
Winsock1.SendData "" + vbCrLf
Winsock1.SendData "" + vbCrLf
Winsock1.SendData "" + vbCrLf
Do While Not EOF(1)
Line Input #1, TData
Winsock1.SendData TData
Loop
ExitThisSub:
Close #1
Command1_Click
Exit Sub
NoRequestFile:
Winsock1.SendData "HTTP/1.0 200 OK" + vbCrLf
Winsock1.SendData "MIME_version:1.0" + vbCrLf
Winsock1.SendData "Content_Type:text/html" + vbCrLf
Winsock1.SendData "Content_Length:" + CStr(30) + vbCrLf
Winsock1.SendData "" + vbCrLf
Winsock1.SendData "" + vbCrLf
Winsock1.SendData "" + vbCrLf
Winsock1.SendData "< br >< br >
< center >< h2 >欢迎您使用VB WebServer,
刚才的地址没有找到!< /h2 >< br >
< br >1999年4月10日< br >< br >
< a href=mailto:liyong@263.net >
勇勇的信箱< /a >< br >< /center >< br >"
Command1_Click
在Winsock1的ConnectRequest事件中加入代码
Winsock1.Close
Winsock1.Accept requestID
至此,大功告成,在c:/inetpub/wwwroot目录中,选一存在的超文本文件,如http://127.0.0.1/default.html即可。程序运行后,会显示出本机的IP地址,启动IE 4.0,在地址栏中输入IP地址及文件名,如果出现上面的画面(略),那么说明服务器运行正常,且IP地址是对的,只是default.html文件不存在。
上一篇:趣味撞球--VB5应用程序一例
下一篇:如何用VB设计动画时钟举例