Public Sub SendFile(FileName As String, WinFunc As Byte, Func As Byte, wins As Winsock)
Dim iPos As Long '判断数据长度
Dim FreeF As Integer '空闲的文件号
Dim LenFile As Long '文件的长度
Dim bytData() As Byte '存放数据的数组
Dim Tempbyt() As Byte '临时变量
Dim Tempstr As String
Dim i As Long
Dim pmax As Long
Dim j As Long
On Error GoTo err:
FreeF = FreeFile '获得空闲的文件号
Open FileName For Binary As #FreeF '打开文件
DoEvents
LenFile = LOF(FreeF) '获得文件长度
' If p_IPRes = 1 Then
' F_IP.Height = 3295
' End If
If LenFile <= iMax Then '如果要发送的文件小于数据块大小,直接发送
If LenFile = 0 Then LenFile = 1
ReDim bytData(LenFile + 11) '根据文件长度重新定义数组大小,具体参见协议
ReDim Tempbyt(LenFile - 1)
bytData(0) = &HAA
bytData(1) = &HBB
bytData(2) = &HCC
bytData(3) = &HDD
bytData(4) = &HEE
bytData(5) = &HFF
bytData(6) = WinFunc
bytData(7) = Func
bytData(8) = &H0 '文件章节总数高位
bytData(9) = &H0
bytData(10) = (LenFile + 12) / 256 '文件章节总数低位
bytData(11) = (LenFile + 12) Mod 256
Get #FreeF, 1, Tempbyt '把文件读入到临时数组里
Close #FreeF '关闭文件
'将读出的数据写入变量
For i = 0 To LenFile - 1
bytData(i + 12) = Tempbyt(i)
Next
wins.SendData bytData '发送数据
Else '文件大于数据块大小,进行分块发送
Do Until iPos >= (LenFile - iMax) '发送整块数据的循环
If iPos = 0 Then
ReDim bytData(iMax + 11)
ReDim Tempbyt(iMax - 1)
bytData(0) = &HAA
bytData(1) = &HBB
bytData(2) = &HCC
bytData(3) = &HDD
bytData(4) = &HEE
bytData(5) = &HFF
bytData(6) = WinFunc
bytData(7) = Func
Tempstr = CStr(Hex(LenFile + 12))
If Len(Tempstr) < 8 Then
For i = 1 To 8 - Len(Tempstr)
Tempstr = "0" & Tempstr
Next
End If
bytData(8) = Val("&H" & Left(Tempstr, 2)) '文件章节总数高位
bytData(9) = Val("&H" & Right(Left(Tempstr, 4), 2))
bytData(10) = Val("&H" & Left(Right(Tempstr, 4), 2)) '文件章节总数低位
bytData(11) = Val("&H" & Right(Tempstr, 2))
' Close #FreeF
Get #FreeF, 1, Tempbyt '把文件读入到临时数组里
'将读出的数据写入变量
For i = 0 To iMax - 1
bytData(i + 12) = Tempbyt(i)
Next
Else
ReDim bytData(iMax - 1)
Get #FreeF, iPos + 1, bytData
End If
wins.SendData bytData
iPos = iPos + iMax '移动iPos,使它指向下来要读的数据
If p_IPRes = 1 Then
F_IP.Label2.Enabled = False
F_IP.ccrpProgressBar1.Visible = True
If F_IP.ccrpProgressBar1.Value <= 100 Then
F_IP.ccrpProgressBar1.Value = Format(iPos / LenFile * 100, "#0.#0")
Else
' Unload F_IP
p_IPRes = 0
End If
End If
Loop
'检查文件有没有剩下的数据,如果文件大小正好等于数据块大小的整数倍,那么就没有剩下的数据了
ReDim bytData(LenFile - iPos - 1) '发送剩下的不够一个数据块的数据
' ReDim Tempbyt(LenFile - iPos - 1)
Get #FreeF, iPos + 1, bytData '把文件读入到临时数组里
Close #FreeF
' '将读出的数据写入变量
' For i = 0 To LenFile - iPos - 1
' bytData(i) = Tempbyt(i)
' Next
'
' bytData(LenFile - iPos) = &HAA
' bytData(LenFile - iPos + 1) = &HBB
' bytData(LenFile - iPos + 2) = &HCC
' bytData(LenFile - iPos + 3) = &HDD
' bytData(LenFile - iPos + 4) = &HEE
' bytData(LenFile - iPos + 5) = &HFF
wins.SendData bytData
End If
p_IPRes = 0
Exit Sub
err:
MsgBox "连接服务器失败,请尝试重新登陆或联系网络管理员!", vbOKOnly + vbExclamation, "提示"
End
End Sub