VB中winsock接收两个数据包有关问题
VB中winsock接收两个数据包问题
在开发一个程序,用winsock进行TCP/IP数据传输,当服务器同时发送回两个包时,只有第一个包能接收到,不知道原因
00 22
01 23 22 32 33 22.。。。。。。。。。
第一行为第一个包能接收到,第二行不知道用什么方法能接收
用网上TCP/IP测试工具是可以接收到的
------解决思路----------------------
仅供参考:
------解决思路----------------------
如果 DataArrival 事件里 bytesTotal = 2,说明第二个包还没送达啊。
那么:到底第二个包发了没有?还是你把 winsock 关了?
在开发一个程序,用winsock进行TCP/IP数据传输,当服务器同时发送回两个包时,只有第一个包能接收到,不知道原因
00 22
01 23 22 32 33 22.。。。。。。。。。
第一行为第一个包能接收到,第二行不知道用什么方法能接收
用网上TCP/IP测试工具是可以接收到的
------解决思路----------------------
仅供参考:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.ocx"
Begin VB.Form Form1
Caption = "vbr"
ClientHeight = 7560
ClientLeft = 9855
ClientTop = 3390
ClientWidth = 9105
LinkTopic = "Form1"
ScaleHeight = 7560
ScaleWidth = 9105
Begin MSWinsockLib.Winsock tcpCAS
Left = 8760
Top = 6720
_ExtentX = 741
_ExtentY = 741
_Version = 393216
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Const MAXLOGFILESIZE As Long = 20000000
Const IBUFSIZE = 131072
Dim iCAS(IBUFSIZE) As Byte
Dim iCASn As Long
Dim CASok As Boolean
Dim CASclosing As Boolean
Dim CASrpc As Long
Dim CASsending As String
Dim INtcpCAS_DataArrival As Boolean
Dim INtcpCAS_DataArrival_TotalBytes As Long
Private Sub Form_Load()
Dim after As Double
ChDrive "C"
ChDir "C:\tmp\test"
tcpCAS.RemoteHost = "127.0.0.1"
tcpCAS.RemotePort = 25200
If tcpCAS.State <> 0 Then
tcpCAS.Close
after = Now + 5# / 24# / 3600#
Do
DoEvents
If tcpCAS.State = 0 Then Exit Do
If Now > after Then
logtofile "Form_Load() wait tcpCAS.state==0 but " + CStr(tcpCAS.State) + " 5s overtime!"
Exit Do
End If
Loop
End If
logtofile "Connect CAS(" + tcpCAS.RemoteHost + ":" + CStr(tcpCAS.RemotePort) + "..."
iCASn = 0&
CASclosing = False
CASok = True
CASsending = ""
tcpCAS.Connect
after = Now + 5# / 24# / 3600#
Do
DoEvents
If tcpCAS.State = 7 Then Exit Do
If Now > after Then
logtofile "FatalError:Connect CAS failure!"
' MsgBox "FatalError:Connect CAS failure!", vbOKOnly
End 'Form
End If
Loop
If tcpCAS.State = 7 Then
logtofile "Connect CAS ok."
' TimerCAStouch.Enabled = True
Else
CASok = False
End If
Form1.Show
End Sub
Private Sub tcpCAS_DataArrival(ByVal bytesTotal As Long)
Dim i As Long
Dim p As Long
Dim qn As Long
Dim s As Long
Dim e As Long
Dim Total_Length As Long
Dim iBuf() As Byte
Dim lnx As String
Const STX = 2
Const ETX = 3
Const LF = 10
Dim L As Long
Dim XORSUM As Long
Dim BYTESUM As Long
Dim strMsgToProcess As String
Dim c As String
Dim PartyID As String
Dim cP As Integer
Dim n As Integer
Dim bt As Long
If INtcpCAS_DataArrival Then
logtofile "ReEnter tcpCAS_DataArrival bytesTotal=" + CStr(bytesTotal)
INtcpCAS_DataArrival_TotalBytes = bytesTotal
Exit Sub
End If
bt = bytesTotal
INtcpCAS_DataArrival = True
REDATA:
logtofile "IN tcpCAS_DataArrival bt=" + CStr(bt)
If bt > IBUFSIZE - iCASn Then
logtofile "Ignore " + CStr(bt - (IBUFSIZE - iCASn)) + " Bytes!"
bt = IBUFSIZE - iCASn
End If
' On Error Resume Next
' 收当前流
ReDim iBuf(bt - 1)
tcpCAS.GetData iBuf, vbArray + vbByte, bt
' logtofile "bt=" + CStr(bt)
' log每个收到的字节
i = 0
lnx = "cas-->BYTE:" + Right("0000000" + Hex(i), 8) + "-"
For i = 0 To bt - 1
lnx = lnx + " " + Right("0" + Hex(iBuf(i)), 2)
If i Mod 16 = 15 Then
logtofile lnx
lnx = "cas-->BYTE:" + Right("0000000" + Hex(i + 1), 8) + "-"
End If
Next
i = bt - 1
If i Mod 16 <> 15 Then
logtofile lnx
End If
' 将本次收到字节放到接收缓冲区末尾
For i = 0 To bt - 1
iCAS(iCASn + i) = iBuf(i)
Next
iCASn = iCASn + bt
' logtofile "iCASn=" + CStr(iCASn)
' 从接收缓冲区中逐个解包
qn = iCASn '剩余要解包字节数
p = 0 '本次解包的首字节
strMsgToProcess = ""
For i = 0 To qn - 1
c = iCAS(p + i)
If c = LF Then c = 124
strMsgToProcess = strMsgToProcess + ChrW(c)
Next
logtofile "pas-->" + strMsgToProcess
n = 0
Do
For i = 0 To qn - 1
If LF = iCAS(p + i) Then Exit For
Next
If i >= qn Then
For i = 0 To qn - 1
iCAS(i) = iCAS(p + i)
Next
iCASn = qn
Exit Do '找不到LF
End If
Total_Length = i + 1
L = i - 1
strMsgToProcess = ""
For i = 0 To L
strMsgToProcess = strMsgToProcess + ChrW(iCAS(p + i))
Next
logtofile "cas-->" + strMsgToProcess
n = n + 1
If n >= 200 Then
logtofile "Wait..."
Wait
n = 0
End If
p = p + Total_Length
qn = qn - Total_Length
If strMsgToProcess = "test9999" Then
logtofile "END"
tcpCAS.Close
End 'From
End If
If qn <= 0 Then
iCASn = 0
Exit Do
End If
Loop
If INtcpCAS_DataArrival_TotalBytes > 0 Then
bt = INtcpCAS_DataArrival_TotalBytes
INtcpCAS_DataArrival_TotalBytes = 0
logtofile "REDATA bt=" + CStr(bt)
GoTo REDATA
End If
INtcpCAS_DataArrival = False
End Sub
Private Sub tcpCAS_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
' On Error Resume Next
logtofile "FatalError:tcpCAS_Error:" + CStr(Number) + "
------解决思路----------------------
" + Description + "
------解决思路----------------------
" + Hex(Scode) + "
------解决思路----------------------
" + Source
CancelDisplay = True
If CASclosing Then Exit Sub
CASclosing = True
CASok = False
Unload Me 'End 'Form
' TimerCASreconn.Enabled = True
End Sub
Private Sub logtofile(s As String)
Dim f As Integer
'On Error Resume Next
f = FreeFile()
Open App.Path + "\VBR1.LOG" For Append As #f
Print #f, Format(Now, "YYYY-MM-DD hh:mm:ss") + " " + s
Close #f
If FileLen(App.Path + "\VBR1.LOG") > MAXLOGFILESIZE Then
Kill App.Path + "\VBR2.LOG"
Name App.Path + "\VBR1.LOG" As App.Path + "\VBR2.LOG"
End If
End Sub
Private Sub Wait()
Dim after As Double
after = Now + 1# / 24# / 3600#
Do
DoEvents '此处tcpCAS_DataArrival会重入
If Now > after Then Exit Do
Loop
End Sub
------解决思路----------------------
如果 DataArrival 事件里 bytesTotal = 2,说明第二个包还没送达啊。
那么:到底第二个包发了没有?还是你把 winsock 关了?