用winsock 收不到信息。

musclecn 2008-07-29 03:05:19
以下是收信息的代码,程序一执行就像进入死循环一样;

<script language="vbscript">
<!--
dim revdata
dim sendata
set sock=createobject("MSWinsock.Winsock")
sock.Protocol=1
sock.Connect "192.168.0.218",11000
do
if sock.BytesReceived>0 then
sock.getdata revdata,vbString
if instr(revdata,"exit") then exit do
else
end if
loop
sock.close
response.Write(revdata)
-->
</script>

以下是发消息的代码,没有问题可以发出:
<%
dim revdata
dim sendata
set sock=createobject("MSWinsock.Winsock")
sock.Protocol=1
sock.Connect "192.168.0.218",11000
sendata="请输入你要发的信息exit"
sock.senddata senddata & chr(13)
sock.close
%>
...全文
158 点赞 收藏 12
写回复
12 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
musclecn 2008-08-04
最终还是放弃试用socket的方法传递数据 怕有丢包。 还是用数据库的方式,就是担心数据库的压力能不能承受
回复
zzxap 2008-07-31
Option Explicit

Public Sub FireEvent(ByVal strClassName, ByVal strEventName, pArray)
Dim strExp, arr, i
strExp = "Call $Class_$Event($Param)"
strExp = Replace(strExp, "$Class", strClassName)
strExp = Replace(strExp, "$Event", strEventName)
ReDim arr(UBound(pArray))
For i = 0 To UBound(arr)
arr(i) = "pArray(" & i & ")"
Next
strExp = Replace(strExp, "$Param", Join(arr, ", "))
Execute strExp
End Sub

Public Const sckClosed = 0 '缺省的。关闭
Public Const sckOpen = 1 '打开
Public Const sckListening = 2 '侦听
Public Const sckConnectionPending = 3 '连接挂起
Public Const sckResolvingHost = 4 '识别主机
Public Const sckHostResolved = 5 '已识别主机
Public Const sckConnecting = 6 '正在连接
Public Const sckConnected = 7 '已连接
Public Const sckClosing = 8 '同级人员正在关闭连接
Public Const sckError = 9 '错误

Dim s, recv, blnSend
blnSend = False
Set s = WScript.CreateObject("MSWinSock.WinSock")
s.Protocol = 0
s.RemoteHost = "www.sohu.com"
s.RemotePort = 80
s.Connect

Do While True
Select Case s.State
Case sckClosed
Case sckOpen
Case sckListening
Case sckConnectionPending
Case sckResolvingHost
Case sckHostResolved
Case sckConnecting
Case sckConnected
If Not blnSend Then
FireEvent "s", "Connect", Array()
Else
Do While s.BytesReceived > 0
FireEvent "s", "DataArrival", Array(s.BytesReceived)
Loop
End If
Case sckClosing
FireEvent "s", "Close", Array()
Exit Do
Case sckError
FireEvent "s", "Error", Array()
Exit Do
End Select
WScript.Sleep 100
Loop
s.Close
Set s = Nothing

Private Sub s_Connect()
WScript.Echo "socket connected"
s.SendData "GET / HTTP/1.1" & vbCrLf & _
"Host: www.sohu.com" & vbCrLf & _
"Connection: Close" & vbCrLf & vbCrLf
blnSend = True
End Sub

Private Sub s_Close()
WScript.Echo "socket closed"
WScript.Echo Replace(recv, Chr(0), "")
End Sub

Private Sub s_DataArrival(ByVal bytesTotal)
Dim tmp
tmp = String(bytesTotal, Chr(0))
s.GetData tmp, vbString, bytesTotal
WScript.Echo "socket recv " & bytesTotal & " bytes"
recv = recv & tmp
End Sub

Private Sub s_Error()
WScript.Echo "socket error"
FireEvent "s", "Close", Array()
End Sub
回复
ymle1228 2008-07-30
UP
回复
wtogether 2008-07-30
因为ASP没有Event,html有Event, ASP发送成功是网速快,不然SendData会出错的,因为还没达到连接状态,sock.State = 7的时候,才可以发送/接收数据包,可以看看这个用VBS模拟的事件,虽然可以接收,但是接收不完全,会丢失数据,你用ASP接收数据也就是这样的情况

Option Explicit

Public Sub FireEvent(ByVal strClassName, ByVal strEventName, pArray)
Dim strExp, arr, i
strExp = "Call $Class_$Event($Param)"
strExp = Replace(strExp, "$Class", strClassName)
strExp = Replace(strExp, "$Event", strEventName)
ReDim arr(UBound(pArray))
For i = 0 To UBound(arr)
arr(i) = "pArray(" & i & ")"
Next
strExp = Replace(strExp, "$Param", Join(arr, ", "))
Execute strExp
End Sub

Public Const sckClosed = 0 '缺省的。关闭
Public Const sckOpen = 1 '打开
Public Const sckListening = 2 '侦听
Public Const sckConnectionPending = 3 '连接挂起
Public Const sckResolvingHost = 4 '识别主机
Public Const sckHostResolved = 5 '已识别主机
Public Const sckConnecting = 6 '正在连接
Public Const sckConnected = 7 '已连接
Public Const sckClosing = 8 '同级人员正在关闭连接
Public Const sckError = 9 '错误

Dim s, recv, blnSend
blnSend = False
Set s = WScript.CreateObject("MSWinSock.WinSock")
s.Protocol = 0
s.RemoteHost = "www.sohu.com"
s.RemotePort = 80
s.Connect

Do While True
Select Case s.State
Case sckClosed
Case sckOpen
Case sckListening
Case sckConnectionPending
Case sckResolvingHost
Case sckHostResolved
Case sckConnecting
Case sckConnected
If Not blnSend Then
FireEvent "s", "Connect", Array()
Else
Do While s.BytesReceived > 0
FireEvent "s", "DataArrival", Array(s.BytesReceived)
Loop
End If
Case sckClosing
FireEvent "s", "Close", Array()
Exit Do
Case sckError
FireEvent "s", "Error", Array()
Exit Do
End Select
WScript.Sleep 100
Loop
s.Close
Set s = Nothing

Private Sub s_Connect()
WScript.Echo "socket connected"
s.SendData "GET / HTTP/1.1" & vbCrLf & _
"Host: www.sohu.com" & vbCrLf & _
"Connection: Close" & vbCrLf & vbCrLf
blnSend = True
End Sub

Private Sub s_Close()
WScript.Echo "socket closed"
WScript.Echo Replace(recv, Chr(0), "")
End Sub

Private Sub s_DataArrival(ByVal bytesTotal)
Dim tmp
tmp = String(bytesTotal, Chr(0))
s.GetData tmp, vbString, bytesTotal
WScript.Echo "socket recv " & bytesTotal & " bytes"
recv = recv & tmp
End Sub

Private Sub s_Error()
WScript.Echo "socket error"
FireEvent "s", "Close", Array()
End Sub
回复
musclecn 2008-07-29
[Quote=引用 5 楼 wtogether 的回复:]
winsock不是这样用的,它是用事务来处理的
[/Quote]

那为什么我发送没有问题啊,我用c#做了个接收程序。发现可以收到asp发的消息。但是我用c#程序发送过来就接收不到。
回复
musclecn 2008-07-29
我试试看
回复
smartcatiboy 2008-07-29
脚本不支持doevent,阻塞应该无效
回复
wtogether 2008-07-29
winsock不是这样用的,它是用事务来处理的
<script language="vbscript">
Dim recv

Private Sub window_OnLoad()
s.Connect
End Sub

Private Sub s_Close()
Debug "socket closed"
Debug "socket recv data<br/>" & HTMLEncode(recv)
End Sub

Private Function HTMLEncode(ByVal strData)
Dim ret
ret = strData
ret = Replace(ret, "&", "&")
ret = Replace(ret, "<", "<")
ret = Replace(ret, ">", ">")
ret = Replace(ret, """", """)
HTMLEncode = ret
End Function

Private Sub s_Connect()
Debug "socket connect"
s.SendData "GET / HTTP/1.1" & vbCrLf & _
"Host: www.sohu.com" & vbCrLf & _
"Connection: Keep-Alive" & vbCrLf & vbCrLf
End Sub

Private Sub s_DataArrival(ByVal bytesTotal)
Dim tmp
tmp = String(bytesTotal, Chr(0))
s.GetData tmp, vbString, bytesTotal
Debug "socket recv " & bytesTotal & " bytes"
recv = recv & tmp
End Sub

Private Sub s_Error(ByVal Number, Description, ByVal Scode, ByVal Source, ByVal HelpFile, ByVal HelpContext, CancelDisplay)
Debug "socket error<br/>" & _
"- Number: " & Hex(Number) & "<br/>" & _
"- description: " & Description
End Sub

Private Sub Debug(ByVal strData)
Dim obj
Set obj = document.getElementById("info")
obj.innerHTML = obj.innerHTML & strData & "<br/>"
Set obj = Nothing
End Sub
'Set s = Nothing
</script>
<div id="info"></div>
<OBJECT ID="s" CLASSID="clsid:{248DD896-BB45-11CF-9ABC-0080C7E7B78D}">
<param name="Protocol" value="0" />
<param name="RemoteHost" value="www.sohu.com" />
<param name="RemotePort" value="80" />
<span>Create Object Error</span>
</OBJECT>
回复
redcn2004 2008-07-29
改成这个 if instr(revdata,"exit")>0 then
回复
musclecn 2008-07-29
判断 接收字符串中是否有exit 有的话就退出循环。
回复
taia 2008-07-29
本来就是个死循环,那个退出的条件有用吗
回复
xiaojing7 2008-07-29
if sock.BytesReceived>0 then 
sock.getdata revdata,vbString
if instr(revdata,"exit") then
exit do
else
end if
loop
sock.close

看不懂,你这块的逻辑!你判断有问题
回复
发帖
ASP
创建于2007-09-28

2.8w+

社区成员

ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
申请成为版主
帖子事件
创建了帖子
2008-07-29 03:05
社区公告
暂无公告