WINSOCK1传送文件问题

tk600158 2007-12-05 05:26:51
写了个WINSOCK传送文件的程序 主要代码如下

发送端

Dim exe() As Byte
Open "d:\1.exe" For Binary As #1
ReDim exe(LOF(1)) As Byte
Get #1, , exe
Close #1
Winsock2.SendData exe

接受端

Dim bxb() As Byte
Winsock1.GetData bxb
Open "d:\2.exe" For Binary As #2
Put #2, , bxb
Close #2
Text2.Text = bxb


传送8K以下文件没问题 可是 8K以上 为什么传送过去就只有8K呢


...全文
48 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
qcjqtvb 2007-12-07
超过8k时,winsock会自动分包
回复
vbman2003 2007-12-07
超过8k时,winsock会自动分包,那个贴子上有示例啊


Open "d:\1.exe" For Binary As #1
ReDim exe(LOF(1)) As Byte
Get #1, , exe
Close #1

'读出数据后不要急于发送,在结尾添加一个标志,供客户端判断是否完整的接收到了数据。比如:

Dim arrLength As Long
arrLength = UBound(exe)
'添加结束标志
dim FinisData() as byte
FinisData = "`!"
ReDim Preserve arrData(arrLength + 4)
CopyMemory exe(arrLength + 1), FinisData(0), 4
'向客户端发送数据
Winsock1(Index).SendData exe
DoEvents


'接收端
Dim bxb() As Byte
Dim Finis As String, FinisData(3) As Byte '结尾标志
Static mData() As Byte '完整的数据
Static mLength As Long
Static iCount As Integer
'接收数据
Winsock1.GetData bxb
'保存分包收到的数据
If iCount = 0 Then '第一次接收
mData = bxb
iCount = 1
Else
ReDim Preserve mData(mLength + bytesTotal)
CopyMemory mData(mLength + 1), bxb(0), bytesTotal
End If
mLength = UBound(mData)

'判断结尾(结尾4个字节)
CopyMemory FinisData(0), mData(mLength - 3), 4
Finis = FinisData
If Finis = "`!" Then
'如果是结尾,截断结尾标志数据
ReDim Preserve mData(mLength - 4)
'写入文件
Open "d:\2.exe" For Binary As #2
Put #2, , mData
Close #2

Erase mData
iCount = 0
mLength = 0
MsgBox "OK"
End If



思路就这样,没有测试,下班了......
回复
tk600158 2007-12-07
可以说说怎么分包不
比如

Open "d:\1.exe" For Binary As #1
ReDim exe(LOF(1)) As Byte
Get #1, , exe
Close #1

现在把二进制数据读到exe数组中间了
怎么实现1次发8K?
我知道 如果这样发送

Open "d:\1.exe" For Binary As #1
ReDim exe(LOF(1)) As Byte
Get #1, , exe
Close #1
for a=0 to ubound(exe)-1
Winsock2.SendData exe
doevents
next

对方写

Dim bxb() As Byte
Winsock1.GetData bxb
Open "d:\2.exe" For Binary As #2
Put #2, , bxb
Close #2
Text2.Text = bxb

这样每次发数组中的一个数据 对方是可以接受的
但是这样1个1M文件起码发半天
谁帮忙优化下


回复
tk600158 2007-12-05
晕 都没人回答啊
回复
tk600158 2007-12-05
不知道怎么弄了 还望高手赐教
回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7486

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2007-12-05 05:26
社区公告
暂无公告