使用VB建立WebServer一例
2006-02-27 11:51:58 来源:WEB开发网一、建立窗体
控件依次为label1,label2,label3,
text1,text2,text3,command1,及Winsock1
label1.caption=”VisualBasicWeb服务器1.0”
label2.caption=”本机地址”
label3.caption=”发布路径”
text2.text=”c:\Inetpub\wwwroot\”
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:liyong@263.net>
勇勇的信箱</a><br></center><br>"
Command1_Click
在Winsock1的ConnectRequest事件中加入代码
Winsock1.Close
Winsock1.AcceptrequestID
至此,大功告成,在c:\inetpub\wwwroot目录中,选一存在的超文本文件,如http://127.0.0.1/default.html即可。程序运行后,会显示出本机的IP地址,启动IE4.0,在地址栏中输入IP地址及文件名,如果出现上面的画面(略),那么说明服务器运行正常,且IP地址是对的,只是default.html文件不存在。
->更多精彩
赞助商链接