为了账号安全,请及时绑定邮箱和手机立即绑定

如果是使用WINSOCK进行TCP连接 连接后如何发送16进制数据包?

如果是使用WINSOCK进行TCP连接 连接后如何发送16进制数据包?

潇潇雨雨 2022-10-27 18:14:24
我这个就需要TCP连接 谢谢!现在问题出现在 :winsock1.sendata "abcde"winsock1.sendata "fghij"他会一次把2个做一个包 全部都发出去我现在需要把这2个分成2个包发送怎么处理?谢谢请教另外的问题截获的数据包是256位的 但是我发送只能发送128位的,例如7F以后的 我发送出去 就不是我原来想发送的哪个数据了,怎么处理?
查看完整描述

3 回答

?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

发送的数据包格式取决于接收端的处理函数接口。
字串格式:直接用字符串发送;
二进制数据:使用字节数组,定义为 Dim abytSend() As Byte,每一个数组元素保存一个字节的十六进制代码(0-255)。
每发一个包,通过sleep函数进行延时处理,或者等于接收端反馈后再发下一个包,或者在两个包之间加上特征字串来分隔。这个根据数据内容和发送要求自己决定吧。

查看完整回答
反对 回复 2022-11-01
?
慕容3067478

TA贡献1773条经验 获得超3个赞

最好就是让对方发送一个反馈包然后发送下一个
或者定义一个数据包的结构,在包头存放标签,数据大小等
然后放数据
最后发包过去
(我自己做的加了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 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 '数据包的标记,可根据需要修改
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


查看完整回答
反对 回复 2022-11-01
?
红糖糍粑

TA贡献1815条经验 获得超6个赞

发送16进制数据包跟发送String类型的数据包是一样的。
或者你也可以转换成2进制发送。
主要是接收端对数据包的处理。

winsock1.sendata "abcde"
Doevents '这里加一句
winsock1.sendata "fghij"

查看完整回答
反对 回复 2022-11-01
  • 3 回答
  • 0 关注
  • 375 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信