VB中发送16进制数据包

2021-08-18 20:42:42 字数 7852 阅读 2337

1楼:匿名用户

发送16进制数据包跟发送string类型的数据包是一样的。

或者你也可以转换成2进制发送。

主要是接收端对数据包的处理。

winsock1.sendata "abcde"

doevents '这里加一句winsock1.sendata "fghij"

2楼:保持距离

发送的数据包格式取决于接收端的处理函数接口。

字串格式:直接用字符串发送;

二进制数据:使用字节数组,定义为 dim abytsend() as byte,每一个数组元素保存一个字节的十六进制**(0-255)。

每发一个包,通过sleep函数进行延时处理,或者等于接收端反馈后再发下一个包,或者在两个包之间加上特征字串来分隔。这个根据数据内容和发送要求自己决定吧。

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

can总线发送数据的问题,can总线中,总结点发送数据,在什么情况下才能把数据发出去,如果发送的数据没人接受,又会发生什么呢??

1楼 胜利之 如果can发送节点处于 正常发送 模式 发送数据但是没有任何节点接收那么一定会发送失败。 一次发送失败不会产生错误中断,只是发送错误计数器值增加,can控制器重发还发不出去,发送错误计数器累加达到一定的值后才会产生相应的中断。 检测是否发送成功要么是通过发送中断或者发送状态位来的,不是...

c语言中原来负数我输出时用想输出为16进制

1楼 匿名用户 看输出的无符号整数最高位是否为1,如果是1则是负数 2楼 匿名用户 x 并不是只是输出正数的 3楼 匿名用户 前面加个负号好了,一 x。 c语言,为什么 o和 x输出的都是无符号数,为什呢不能有负数 4楼 匿名用户 不是说不能有负数,如果使用 x和 o输出时,应该先把你的数据转为二进...

在微信上给朋友发红包限制款数吗,哪些微信的公众号会发送微信红包?

1楼 匿名用户 有限制钱数,最多可发单个红包200元,超出不能发了可以再发一次, 哪些微信的 会发送微信红包? 2楼 言南絮 关注领红包 口令红包 功能特点 操作简单,5分钟即可搞定功能设置实现发红包。 摇一摇红包 摇钱树订阅号摇红包 功能特点 既未影响活动的互动效果,且又增加了订阅号的用户量 摇一...