一、建立窗體
控件依次為label1,label2,label3,
text1,text2,text3,command1,及Winsock1
label1.caption=”VisualBasicWeb服務器1.0”
label2.caption=”本機地址”
label3.caption=”發布路徑”
text2.text=”c:Inetpubwwwroot”
command1.caption=”啟動”
又擊Command1加入代碼
PrivateSubCommand1_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)
EndSub
然後在Winsock1的DataArrival事件中加入代碼
Winsock1.GetDataDataReceived,vbString
Text3.Text=DataReceived
pos1=0
pos2=0
Fori=1ToLen(DataReceived)
IfMid(DataReceived,i,1)=""Then
Ifpos1=0Then
pos1=i 1
Else
pos2=i-1
ExitFor
EndIf
EndIf
Next
OnErrorGoToExitThisSub
Ifpos1<>0Andpos2<>0Then
requestfilename=Mid(DataReceived,pos1 1,pos2-pos1)
requestfilename=Trim(Text2.Text)&requestfilename
IfDir(requestfilename)<>""Then
Else
GoToNoRequestFile
EndIf
EndIf
OpenrequestfilenameForInputAs#1
Winsock1.SendData"HTTP/1.0200OK" 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
DoWhileNotEOF(1)
LineInput#1,TData
Winsock1.SendDataTData
Loop
ExitThisSub:
Close#1
Command1_Click
ExitSub
NoRequestFile:
Winsock1.SendData"HTTP/1.0200OK" 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>歡迎您使用VBWebServer,
剛才的地址沒有找到!</h2><br>
<br>1999年4月10日<br><br>
<ahref=mailto:[email protected]>
勇勇的信箱</a><br></center><br>"
Command1_Click
在Winsock1的ConnectRequest事件中加入代碼
Winsock1.Close
Winsock1.AcceptrequestID
至此,大功告成,在c:inetpubwwwroot目錄中,選一存在的超文本文件,如http://127.0.0.1/default.html即可。程序運行後,會顯示出本機的IP地址,啟動IE4.0,在地址欄中輸入IP地址及文件名,如果出現上面的畫面(略),那麼說明服務器運行正常,且IP地址是對的,只是default.html文件不存在。
->