基于VB的winsock控件实现web访问
大家好,我是VB的初学者。我的程序只能获取本地IP,可是打不开。html文件,求各位大虾指导,看看哪里有问题,很急哦,谢谢啦……Private Sub Command1_Click()
DoEvents
Winsock1.Close
Winsock1.Protocol = sckTCPProtocol
Winsock1.LocalPort = 80
Winsock1.Listen
Text1.Text = CStr(Winsock1.LocalIP)
End Sub '设置WINSOCK,开始监听
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Accept requestID
End Sub '接受请求
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
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 '这里的代码不很清楚,总体是和下面
'得出requestfilename的位置和名字
On Error GoTo exitthissub
If post <> 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 " < center > < h2 > 欢迎使用! < /h2 > < br > < br > < a href=mailto:44023154@163.com > 信箱 < /a > < br > < /center > < br > " '无文件时返回
Command1_Click
End Sub
--------------------编程问答-------------------- winsock是底层协议,要读取html还需要使用http协议(应用层) --------------------编程问答-------------------- 那程序应该怎么编写啊,有参考的吗?急求……
补充:VB , 控件