慕容3067478
最好就是让对方发送一个反馈包然后发送下一个或者定义一个数据包的结构,在包头存放标签,数据大小等然后放数据最后发包过去(我自己做的加了MD5验证)'这是接收数据的winsock'Execute函数是处理数据的:Private Function Execute(ID As Integer, b() As Byte, ret As Boolean) As Byte()'ret是是否返回发送一个数据包,b()是数据,ID是winsock的index(这是支持多sock的)Private Sub wsi_DataArrival(Index As Integer, ByVal bytesTotal As Long)'Debug.Print bytesTotal'If bytesTotal = 2 Then Stop'Debug.Print UBound(buff(bi).b)If bytesTotal <= 0 Then Exit SubDim bi As Integer, br As Long, kh As kHeader, kf As kFooterbi = FindIDBuff(Index + 1)If bi = -1 Thenbi = NewBuffindexbuff(bi).ID = Index + 1Dim t() As ByteReDim t(LenB(kh) - 1) As Bytewsi(Index).GetData t, , LenB(kh)br = br + LenB(kh)Debug.Print StrConv(t, vbUnicode)CopyMemory kh, t(0), LenB(kh)If IsKHValid(kh) ThenDebug.Print kh.DatalenDebug.Print t(7)'copymemory t(0)buff(bi).Length = kh.DatalenIf kh.Datalen + br > bytesTotal ThenReDim buff(bi).b(bytesTotal - br - 1) As Byte'Debug.Print UBound(buff(bi).b)wsi(Index).GetData buff(bi).b, , bytesTotal - brbr = bytesTotalElseIf kh.Datalen + br + LenB(kf) > bytesTotal ThenReDim buff(bi).b(kh.Datalen - 1) As Bytewsi(Index).GetData buff(bi).b, , kh.Datalenbr = br + kh.DatalenReDim buff(bi).b1(bytesTotal - br - 1) As Bytewsi(Index).GetData buff(bi).b1, , bytesTotal - brbr = bytesTotalElseReDim buff(bi).b(kh.Datalen - 1) As Bytewsi(Index).GetData buff(bi).b, , kh.DatalenReDim t(LenB(kf) - 1) As Bytewsi(Index).GetData t, , LenB(kf)CopyMemory kf, t(0), LenB(kf)Dim r As Boolean, rb() As ByteIf IsKFValid(kf, buff(bi).b) Thenrb = Execute(CInt(kh.ID), buff(bi).b, r)buff(bi).ID = 0If r = True Then wsi(Index).SendData rbEnd IfEnd IfElse'invalid= =buff(bi).ID = 0Call wsi_DataArrival(Index, bytesTotal - br)End IfElse'there is buffIf buff(bi).Length > UBound(buff(bi).b) + 1 Then 'loading buff0If buff(bi).Length - UBound(buff(bi).b) - 1 >= bytesTotal ThenDim t_ub As Long, t_b() As Bytet_ub = UBound(buff(bi).b)ReDim Preserve buff(bi).b(UBound(buff(bi).b) + bytesTotal) As ByteReDim t_b(bytesTotal - 1) As Bytewsi(Index).GetData t_b, , bytesTotalbr = bytesTotalDebug.Print UBound(t_b)CopyMemory buff(bi).b(t_ub + 1), t_b(0), bytesTotalElset_ub = UBound(buff(bi).b)ReDim Preserve buff(bi).b(buff(bi).Length - 1) As ByteReDim t_b(buff(bi).Length - t_ub - 2) As ByteDebug.Print UBound(t_b)wsi(Index).GetData t_b, , buff(bi).Length - t_ub - 1br = UBound(t_b) + 1CopyMemory buff(bi).b(t_ub + 1), t_b(0), buff(bi).Length - t_ub - 1If bytesTotal - br >= LenB(kf) ThenReDim t(LenB(kf) - 1) As Bytewsi(Index).GetData t, , LenB(kf)br = br + LenB(kf)CopyMemory kf, t(0), LenB(kf)If IsKFValid(kf, buff(bi).b) Thenrb = Execute(CInt(kh.ID), buff(bi).b, r)buff(bi).ID = 0If r = True Then wsi(Index).SendData rbEnd IfIf bytesTotal - br > 0 Then Call wsi_DataArrival(Index, bytesTotal - br)ElseReDim buff(bi).b1(bytesTotal - br - 1) As Bytewsi(Index).GetData buff(bi).b1, , bytesTotal - brbr = bytesTotalEnd IfEnd IfElse 'loading buff1Dim tch As Longtch = -1On Error Resume Nexttch = UBound(buff(bi).b1)If LenB(kf) - tch - 1 >= bytesTotal Thent_ub = tchReDim buff(bi).b1(LenB(kf) - 1) As Bytewsi(Index).GetData buff(bi).b1(t_ub + 1), , LenB(kf) - t_ub - 1Elset_ub = tchReDim buff(bi).b1(tch + bytesTotal) As Bytewsi(Index).GetData buff(bi).b1(t_ub + 1), , bytesTotalEnd IfEnd IfEnd IfEnd Sub'''''''一个模块option explicitPublic Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Public Type Bufferb() As Byteb1() As ByteID As IntegerLength As LongEnd TypePublic buff() As BufferPublic Const ver1 As Byte = 1'版本..可能以后会用到Public Const ver2 As Byte = 0Public Const signa As Long = &H4B501111 '数据包的标记,可根据需要修改Public Type kHeaderstart As Byteversion(1) As Bytesign As LongID As LongDatalen As LongEnd TypePublic Type kFooterMD5(15) As Bytesign As Longend As ByteEnd TypePublic Declare Sub MD5Final Lib "advapi32" (ctx As MD5CONTEXT)Public Declare Sub MD5Init Lib "advapi32" (ctx As MD5CONTEXT)Public Declare Sub MD5Update Lib "advapi32" (ctx As MD5CONTEXT, buf As Any, ByVal Length As Long)Public Type MD5CONTEXTi(1) As Longbuf(3) As Longinp(63) As Bytedigest(15) As ByteEnd TypePublic Const MM_JOIN As Byte = &H10Public Const MMRET_JOIN As Byte = &HE0Public Const MM_JOINREQINFO As Byte = MM_JOIN + &H1Public Const MMRET_JOINREQINFO As Byte = MMRET_JOIN + &H1Public Const MM_ECHO = &HFFPublic Function MD5Hash(b() As Byte) As Byte()Dim m5c As MD5CONTEXTMD5Init m5cMD5Update m5c, b(0), UBound(b) + 1MD5Final m5cMD5Hash = m5c.digestEnd FunctionPublic Function PackUp(ID As Integer, b() As Byte) As Byte()Dim p() As Byte, K As kHeader, kf As kFooterReDim p(UBound(b) + LenB(K) + LenB(kf)) As ByteK.start = &H2K.version(0) = ver1K.version(1) = ver2K.sign = signaK.ID = IDK.Datalen = UBound(b) + 1CopyMemory p(0), K, LenB(K)CopyMemory p(LenB(K)), b(0), UBound(b) + 1Dim tb() As Bytetb = MD5Hash(b)CopyMemory kf.MD5(0), tb(0), 16kf.sign = signakf.end = &H3CopyMemory p(UBound(p) - LenB(kf) + 1), kf, LenB(kf)PackUp = pEnd FunctionPublic Function RawJoinReqInfo(ByVal ID As Integer) As Byte()Dim p(2) As Bytep(0) = MM_JOINREQINFOCopyMemory p(1), ID, 2RawJoinReqInfo = pEnd FunctionPublic Function IsKHValid(K As kHeader) As BooleanIf K.start = &H2 And K.sign = signa ThenIsKHValid = TrueEnd IfEnd FunctionPublic Function IsKFValid(K As kFooter, b() As Byte) As BooleanDebug.Print b(1)If K.end = &H3 And K.sign = signa And bequal(K.MD5, MD5Hash(b)) ThenIsKFValid = TrueEnd IfEnd FunctionPrivate Function bequal(b1() As Byte, b2() As Byte) As BooleanDim i& ', cmin&'cmin = Abs(UBound(b1) + UBound(b2)) - Abs(UBound(b1) - UBound(b2)) / 2Debug.Print b1If UBound(b1) - LBound(b1) <> UBound(b2) - LBound(b2) Then Exit FunctionFor i = 0 To UBound(b1)If b1(LBound(b1) + i) <> b2(LBound(b2) + i) Then Exit FunctionNext ibequal = TrueEnd Function