3 回答

TA貢獻1757條經(jīng)驗 獲得超8個贊
發(fā)送的數(shù)據(jù)包格式取決于接收端的處理函數(shù)接口。
字串格式:直接用字符串發(fā)送;
二進制數(shù)據(jù):使用字節(jié)數(shù)組,定義為 Dim abytSend() As Byte,每一個數(shù)組元素保存一個字節(jié)的十六進制代碼(0-255)。
每發(fā)一個包,通過sleep函數(shù)進行延時處理,或者等于接收端反饋后再發(fā)下一個包,或者在兩個包之間加上特征字串來分隔。這個根據(jù)數(shù)據(jù)內容和發(fā)送要求自己決定吧。

TA貢獻1773條經(jīng)驗 獲得超3個贊
最好就是讓對方發(fā)送一個反饋包然后發(fā)送下一個
或者定義一個數(shù)據(jù)包的結構,在包頭存放標簽,數(shù)據(jù)大小等
然后放數(shù)據(jù)
最后發(fā)包過去
(我自己做的加了MD5驗證)
'這是接收數(shù)據(jù)的winsock
'Execute函數(shù)是處理數(shù)據(jù)的:Private Function Execute(ID As Integer, b() As Byte, ret As Boolean) As Byte()
'ret是是否返回發(fā)送一個數(shù)據(jù)包,b()是數(shù)據(jù),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 Sub
Dim bi As Integer, br As Long, kh As kHeader, kf As kFooter
bi = FindIDBuff(Index + 1)
If bi = -1 Then
bi = NewBuffindex
buff(bi).ID = Index + 1
Dim t() As Byte
ReDim t(LenB(kh) - 1) As Byte
wsi(Index).GetData t, , LenB(kh)
br = br + LenB(kh)
Debug.Print StrConv(t, vbUnicode)
CopyMemory kh, t(0), LenB(kh)
If IsKHValid(kh) Then
Debug.Print kh.Datalen
Debug.Print t(7)
'copymemory t(0)
buff(bi).Length = kh.Datalen
If kh.Datalen + br > bytesTotal Then
ReDim buff(bi).b(bytesTotal - br - 1) As Byte
'Debug.Print UBound(buff(bi).b)
wsi(Index).GetData buff(bi).b, , bytesTotal - br
br = bytesTotal
ElseIf kh.Datalen + br + LenB(kf) > bytesTotal Then
ReDim buff(bi).b(kh.Datalen - 1) As Byte
wsi(Index).GetData buff(bi).b, , kh.Datalen
br = br + kh.Datalen
ReDim buff(bi).b1(bytesTotal - br - 1) As Byte
wsi(Index).GetData buff(bi).b1, , bytesTotal - br
br = bytesTotal
Else
ReDim buff(bi).b(kh.Datalen - 1) As Byte
wsi(Index).GetData buff(bi).b, , kh.Datalen
ReDim t(LenB(kf) - 1) As Byte
wsi(Index).GetData t, , LenB(kf)
CopyMemory kf, t(0), LenB(kf)
Dim r As Boolean, rb() As Byte
If IsKFValid(kf, buff(bi).b) Then
rb = Execute(CInt(kh.ID), buff(bi).b, r)
buff(bi).ID = 0
If r = True Then wsi(Index).SendData rb
End If
End If
Else
'invalid= =
buff(bi).ID = 0
Call wsi_DataArrival(Index, bytesTotal - br)
End If
Else
'there is buff
If buff(bi).Length > UBound(buff(bi).b) + 1 Then 'loading buff0
If buff(bi).Length - UBound(buff(bi).b) - 1 >= bytesTotal Then
Dim t_ub As Long, t_b() As Byte
t_ub = UBound(buff(bi).b)
ReDim Preserve buff(bi).b(UBound(buff(bi).b) + bytesTotal) As Byte
ReDim t_b(bytesTotal - 1) As Byte
wsi(Index).GetData t_b, , bytesTotal
br = bytesTotal
Debug.Print UBound(t_b)
CopyMemory buff(bi).b(t_ub + 1), t_b(0), bytesTotal
Else
t_ub = UBound(buff(bi).b)
ReDim Preserve buff(bi).b(buff(bi).Length - 1) As Byte
ReDim t_b(buff(bi).Length - t_ub - 2) As Byte
Debug.Print UBound(t_b)
wsi(Index).GetData t_b, , buff(bi).Length - t_ub - 1
br = UBound(t_b) + 1
CopyMemory buff(bi).b(t_ub + 1), t_b(0), buff(bi).Length - t_ub - 1
If bytesTotal - br >= LenB(kf) Then
ReDim t(LenB(kf) - 1) As Byte
wsi(Index).GetData t, , LenB(kf)
br = br + LenB(kf)
CopyMemory kf, t(0), LenB(kf)
If IsKFValid(kf, buff(bi).b) Then
rb = Execute(CInt(kh.ID), buff(bi).b, r)
buff(bi).ID = 0
If r = True Then wsi(Index).SendData rb
End If
If bytesTotal - br > 0 Then Call wsi_DataArrival(Index, bytesTotal - br)
Else
ReDim buff(bi).b1(bytesTotal - br - 1) As Byte
wsi(Index).GetData buff(bi).b1, , bytesTotal - br
br = bytesTotal
End If
End If
Else 'loading buff1
Dim tch As Long
tch = -1
On Error Resume Next
tch = UBound(buff(bi).b1)
If LenB(kf) - tch - 1 >= bytesTotal Then
t_ub = tch
ReDim buff(bi).b1(LenB(kf) - 1) As Byte
wsi(Index).GetData buff(bi).b1(t_ub + 1), , LenB(kf) - t_ub - 1
Else
t_ub = tch
ReDim buff(bi).b1(tch + bytesTotal) As Byte
wsi(Index).GetData buff(bi).b1(t_ub + 1), , bytesTotal
End If
End If
End If
End Sub
''''''
'一個模塊
option explicit
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Type Buffer
b() As Byte
b1() As Byte
ID As Integer
Length As Long
End Type
Public buff() As Buffer
Public Const ver1 As Byte = 1'版本..可能以后會用到
Public Const ver2 As Byte = 0
Public Const signa As Long = &H4B501111 '數(shù)據(jù)包的標記,可根據(jù)需要修改
Public Type kHeader
start As Byte
version(1) As Byte
sign As Long
ID As Long
Datalen As Long
End Type
Public Type kFooter
MD5(15) As Byte
sign As Long
end As Byte
End Type
Public 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 MD5CONTEXT
i(1) As Long
buf(3) As Long
inp(63) As Byte
digest(15) As Byte
End Type
Public Const MM_JOIN As Byte = &H10
Public Const MMRET_JOIN As Byte = &HE0
Public Const MM_JOINREQINFO As Byte = MM_JOIN + &H1
Public Const MMRET_JOINREQINFO As Byte = MMRET_JOIN + &H1
Public Const MM_ECHO = &HFF
Public Function MD5Hash(b() As Byte) As Byte()
Dim m5c As MD5CONTEXT
MD5Init m5c
MD5Update m5c, b(0), UBound(b) + 1
MD5Final m5c
MD5Hash = m5c.digest
End Function
Public Function PackUp(ID As Integer, b() As Byte) As Byte()
Dim p() As Byte, K As kHeader, kf As kFooter
ReDim p(UBound(b) + LenB(K) + LenB(kf)) As Byte
K.start = &H2
K.version(0) = ver1
K.version(1) = ver2
K.sign = signa
K.ID = ID
K.Datalen = UBound(b) + 1
CopyMemory p(0), K, LenB(K)
CopyMemory p(LenB(K)), b(0), UBound(b) + 1
Dim tb() As Byte
tb = MD5Hash(b)
CopyMemory kf.MD5(0), tb(0), 16
kf.sign = signa
kf.end = &H3
CopyMemory p(UBound(p) - LenB(kf) + 1), kf, LenB(kf)
PackUp = p
End Function
Public Function RawJoinReqInfo(ByVal ID As Integer) As Byte()
Dim p(2) As Byte
p(0) = MM_JOINREQINFO
CopyMemory p(1), ID, 2
RawJoinReqInfo = p
End Function
Public Function IsKHValid(K As kHeader) As Boolean
If K.start = &H2 And K.sign = signa Then
IsKHValid = True
End If
End Function
Public Function IsKFValid(K As kFooter, b() As Byte) As Boolean
Debug.Print b(1)
If K.end = &H3 And K.sign = signa And bequal(K.MD5, MD5Hash(b)) Then
IsKFValid = True
End If
End Function
Private Function bequal(b1() As Byte, b2() As Byte) As Boolean
Dim i& ', cmin&
'cmin = Abs(UBound(b1) + UBound(b2)) - Abs(UBound(b1) - UBound(b2)) / 2
Debug.Print b1
If UBound(b1) - LBound(b1) <> UBound(b2) - LBound(b2) Then Exit Function
For i = 0 To UBound(b1)
If b1(LBound(b1) + i) <> b2(LBound(b2) + i) Then Exit Function
Next i
bequal = True
End Function

TA貢獻1815條經(jīng)驗 獲得超6個贊
發(fā)送16進制數(shù)據(jù)包跟發(fā)送String類型的數(shù)據(jù)包是一樣的。
或者你也可以轉換成2進制發(fā)送。
主要是接收端對數(shù)據(jù)包的處理。
winsock1.sendata "abcde"
Doevents '這里加一句
winsock1.sendata "fghij"
- 3 回答
- 0 關注
- 423 瀏覽
添加回答
舉報