?? vb1.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0056)http://www.pconline.com.cn/experience/subject/VB/vb1.htm -->
<HTML><HEAD><TITLE>|><| 太平洋電腦信息網</TITLE><!-- #BeginTemplate "/Templates/0723.dwt" --><!-- #BeginEditable "doctitle" --><!-- #EndEditable -->
<META content="text/html; charset=gb2312" http-equiv=Content-Type><LINK
href="vb1.files/text.css" rel=stylesheet>
<STYLE type=text/css>.sfont {
LINE-HEIGHT: 18pt
}
</STYLE>
<META content="MSHTML 5.00.2014.210" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff leftMargin=0 topMargin=0>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width="100%">
<TBODY>
<TR>
<TD vAlign=top width="100%">
<TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
<TBODY>
<TR bgColor=#303880>
<TD><A href="http://www.pconline.com.cn/"><IMG alt=太平洋電腦信息網 border=0
height=18 hspace=30 src="vb1.files/txtpconline.gif"
width=96></A><IMG height=10 src="vb1.files/blank.gif"
width=400></TD></TR></TBODY></TABLE></TD>
<TD align=right background=vb1.files/bw.gif noWrap width=130><IMG
height=26 src="vb1.files/bw.gif" width=10><A
href="http://www.pconline.com.cn/sitemap/"><IMG alt=網站地圖 border=0
height=26 src="vb1.files/sitemap.gif" width=21></A><IMG height=26
src="vb1.files/bw.gif" width=10><A
href="http://www.pconline.com.cn/tpy/"><IMG alt=關于太平洋 border=0 height=26
src="vb1.files/aboutus.gif" width=21></A><IMG height=26
src="vb1.files/bw.gif" width=10><A
href="http://www.pconline.com.cn/tpy/tariff.html"><IMG alt=與我們聯系 border=0
height=26 src="vb1.files/contact.gif" width=21></A><IMG height=26
src="vb1.files/bw.gif" width=10></TD></TR></TBODY></TABLE><BR>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=760>
<TBODY>
<TR>
<TD bgColor=#666666><IMG height=1 src="vb1.files/blank.gif" width=1></TD>
<TD bgColor=#666666><IMG height=1 src="vb1.files/blank.gif" width=1></TD>
<TD> </TD>
<TD colSpan=2 noWrap><A
href="http://www.pconline.com.cn/experience/index.html"><IMG
align=absBottom alt=企業之窗 border=0 height=15
src="vb1.files/txtexperience.gif" width=68></A> <A
href="http://www.pconline.com.cn/">首 頁</A> <A
href="http://www.pconline.com.cn/market/">今日報價</A> <A
href="http://www.pconline.com.cn/recommend/">產品推介</A> <A
href="http://www.pconline.com.cn/yp/">企業之窗</A> <A
href="http://www.pconline.com.cn/news/">科技新聞</A> <A
href="http://www.pconline.com.cn/product/">產品查詢</A> <A
href="http://www.pconline.com.cn/download/">軟件下載</A></TD></TR>
<TR>
<TD><IMG height=1 src="vb1.files/blank.gif" width=1></TD>
<TD><IMG height=1 src="vb1.files/blank.gif" width=1></TD>
<TD><IMG height=1 src="vb1.files/blank.gif" width=21></TD>
<TD bgColor=#ffa000 colSpan=2><IMG height=1 src="vb1.files/blank.gif"
width=1></TD></TR>
<TR>
<TD bgColor=#666666><IMG height=1 src="vb1.files/blank.gif" width=1></TD>
<TD bgColor=#cdcdcd><IMG height=5 src="vb1.files/blank.gif" width=1></TD>
<TD align=right colSpan=3><IMG height=6 src="vb1.files/ffa000.gif"
width=1></TD></TR>
<TR>
<TD bgColor=#666666><IMG height=1 src="vb1.files/blank.gif" width=1></TD>
<TD align=right><A href="http://www.pconline.com.cn/"><IMG alt=太平洋電腦信息網
border=0 height=35 src="vb1.files/pconline.com.cn.gif" vspace=10
width=162></A></TD>
<TD> </TD>
<TD vAlign=bottom><!-- #BeginEditable "important_news" --><IMG height=40
src="vb1.files/blank.gif" width=350><!-- #EndEditable --></TD>
<TD align=right vAlign=top><!-- #BeginEditable "adv_top" --><IMG alt=廣告位
height=43 src="vb1.files/icon.gif"
width=190><!-- #EndEditable --></TD></TR></TBODY></TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=760>
<TBODY>
<TR>
<TD bgColor=#666666 width=1><IMG height=1 src="vb1.files/blank.gif"
width=1></TD>
<TD vAlign=top width=170><!-- #BeginEditable "addons" --><!--替代為附加信息 --><!-- #EndEditable --></TD>
<TD bgColor=#666666 width=1><IMG height=1 src="vb1.files/blank.gif"
width=1></TD>
<TD><IMG height=1 src="vb1.files/blank.gif" width=21></TD>
<TD colSpan=3 vAlign=top width=566><!-- #BeginEditable "1" --><IMG
height=1 src="vb1.files/blank.gif" width=566><BR><IMG height=1
src="vb1.files/666666.gif" width=420><BR><IMG height=13
src="vb1.files/blank.gif" width=1><A
href="http://www.pconline.com.cn/experience/">經驗共享</A> > <SPAN
class=greenb><A
href="http://www.pconline.com.cn/experience/subject/VB/index.html">專題教程</A></SPAN><A
href="http://www.pconline.com.cn/experience/subject/VB/index.html">
VB專區</A>> <BR><IMG height=1 src="vb1.files/666666.gif" vspace=1
width=420><BR>
<P align=center><B><FONT color=#000066><SPAN class=sfont>使用VB建立Web
Server</SPAN></FONT></B><SPAN class=sfont><BR></SPAN></P>
<P><SPAN class=sfont>李勇 </SPAN></P>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
HTTP協議在瀏覽器中使用的原理:首先,Web瀏覽器與服務器建立連接,然后Web瀏覽器通過HTTP協議向服務器請求文檔,最后,由服務器向Web瀏覽器應答,關閉連接。這就是HTTP協議的一般工作過程。下面,使用Visual
Basic 5.0中文版來實現一個簡單的Web服務器,使用Tcp/IP協議的
80端口,一般瀏覽器中的Http協議默認此端口,然后設置好超文本文件的發布路徑,本程序默認為C:\InetPub\wwwroot,此Web
Server程序比較簡單,只適用于包含有文的超文本文件,如果超文本文件中含有圖形,無法顯示出來。 </SPAN></P><SPAN
class=sfont><B>一、建立窗體</B> </SPAN><PRE><SPAN class=sfont>
控件依次為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
</SPAN></PRE>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
至此,大功告成,在c:\inetpub\wwwroot目錄中,選一存在的超文本文件,如http://127.0.0.1/default.html即可。程序運行后,會顯示出本機的IP地址,啟動IE
4.0,在地址欄中輸入IP地址及文件名,如果出現上面的畫面(略),那么說明服務器運行正常,且IP地址是對的,只是default.html文件不存在。
</SPAN></P>
<P align=right><A
href="http://www.pconline.com.cn/experience/subject/VB/index.html">----更多的文章</A>
</P><!-- #EndEditable --></TD></TR></TBODY></TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=760>
<TBODY>
<TR>
<TD bgColor=#666666 width=1><IMG height=1 src="vb1.files/blank.gif"
width=1></TD>
<TD align=right vAlign=bottom width=170><IMG height=10
src="vb1.files/blank.gif" width=170><BR></TD>
<TD bgColor=#666666 width=1><IMG height=1 src="vb1.files/blank.gif"
width=1></TD>
<TD width=21><IMG height=1 src="vb1.files/blank.gif" width=21></TD>
<TD bgColor=#eeeeee width=566><IMG height=10 src="vb1.files/blank.gif"
width=566><BR>版權所有©1999 太平洋電腦信息網<BR><FONT
face="Arial, Helvetica, sans-serif"><A
href="mailto:exp@pconline.com.cn">exp@pconline.com.cn</A></FONT> <IMG
height=1 src="vb1.files/hhvisit.html" width=1> </TD></TR></TBODY></TABLE><!-- #EndTemplate --></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -