📄 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 + -