PutFile 用来传送文件,数据流的格式是:第一个字节代表传送的是文件(因为在我的程序中我还要传送确认,用这个字节以示区分);第二个字节代表文件名长度,以后是文件名、数据块长度、数据块。
Dim bRecieved As Boolean
Private Sub tcpServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim byteData() As Byte, rsResult As ADODB.Recordset
If bytesTotal > 0 Then
ReDim byteData(bytesTotal - 1)
tcpServer(Index).GetData byteData, vbByte + vbArray
Select Case byteData(0)
Case 0
bRecieved = True
Case 1
SaveFile byteData
tcpServer(Index).SendData CByte(0)
End Select
End If
End Sub
Private Sub PutFile(objSendSock As Winsock, sFileName As String)
Dim iThisFile As Integer, byteData() As Byte
Dim i As Long, j As Long, byteBuffer() As Byte
Dim iBlockCount As Integer, lDivValue As Long
' On Error GoTo errPutFile
iThisFile = FreeFile
Open sFileName For Binary As #iThisFile
ReDim byteData(Len(sFileName) + 3)
byteData(0) = 1
byteData(1) = CByte(Len(sFileName))
For j = 1 To Len(sFileName)
byteData(j + 1) = CByte(Asc(Mid(sFileName, j, 1)))
Next
iBlockCount = LOF(iThisFile) \ BufferSize
For i = 0 To 1
byteData(i + 2 + Len(sFileName)) = CByte((iBlockCount \ 2 ^ (i * 8)) And &HFF)
Next
For i = 1 To iBlockCount
ReDim Preserve byteData(BufferSize + 10 + Len(sFileName))
For j = 0 To 1 '计算当前帧号
byteData(j + 4 + Len(sFileName)) = CByte((i \ 2 ^ (j * 8)) And &HFF)
Next
For j = 0 To 3 '计算数据块长度
byteData(j + 6 + Len(sFileName)) = CByte((BufferSize \ 2 ^ (j * 8)) And &HFF)
Next
ReDim byteBuffer(BufferSize - 1)
Get #iThisFile, , byteBuffer
For j = 0 To BufferSize - 1
byteData(j + 10 + Len(sFileName)) = byteBuffer(j)
Next
bRecieved = False
objSendSock.SendData byteData
Do While Not bRecieved
DoEvents
Loop
Next
lDivValue = LOF(iThisFile) Mod BufferSize
If lDivValue <> 0 Then
ReDim Preserve byteData(lDivValue + 10 + Len(sFileName))
For j = 0 To 1
byteData(j + 4 + Len(sFileName)) = CByte((i \ 2 ^ (j * 8)) And &HFF)
Next
For j = 0 To 3
byteData(j + 6 + Len(sFileName)) = CByte((lDivValue \ 2 ^ (j * 8)) And &HFF)
Next
ReDim byteBuffer(lDivValue - 1)
Get #iThisFile, , byteBuffer
For j = 0 To lDivValue - 1
byteData(j + 10 + Len(sFileName)) = byteBuffer(j)
Next
bRecieved = False
objSendSock.SendData byteData
Do While Not bRecieved
DoEvents
Loop
End If
Close #iThisFile
Exit Sub
errPutFile:
Select Case Err.Number
Case Else
MsgBox Err.Number & ":" & Err.Description, vbInformation, "PutFile 错误提示"
End Select
End Sub
Private Sub SaveFile(byteData() As Byte)
Dim lSize As Long, i As Integer, strTemp As String
Dim sFileName As String, byteBuffer() As Byte
Dim iThisFile As Integer, lSoFarIn As Long
Dim iDotPos As Integer
Dim iBlockIndex As Integer, iBlockCount As Integer
'On Error GoTo errSaveData
sFileName = ""
For i = 1 To byteData(1)
sFileName = sFileName & Chr(byteData(i + 1))
Next
For i = 0 To 1
iBlockCount = iBlockCount + byteData(i + 2 + byteData(1)) * 2 ^ (i * 8)
Next
For i = 0 To 1
iBlockIndex = iBlockIndex + byteData(i + 4 + byteData(1)) * 2 ^ (i * 8)
Next
If iBlockIndex = 1 Then
i = 0
strTemp = sFileName
Do While Dir(strTemp) <> ""
i = i + 1
strTemp = sFileName
iDotPos = InStr(strTemp, ".")
strTemp = Left(strTemp, iDotPos - 1) & "_" & i & Right(strTemp, Len(strTemp) - iDotPos + 1)
Loop
If Dir(sFileName) <> "" Then
FileCopy sFileName, strTemp
Kill sFileName
End If
End If
For i = 0 To 3
lSize = lSize + byteData(i + byteData(1) + 6) * 2 ^ (i * 8)
Next
ReDim byteBuffer(lSize - 1)
For i = 0 To lSize - 1
byteBuffer(i) = byteData(i + byteData(1) + 10)
Next
iThisFile = FreeFile
Open sFileName For Binary As iThisFile
lSoFarIn = LOF(iThisFile) + 1
Put #iThisFile, lSoFarIn, byteBuffer
Close #iThisFile
Exit Sub
errSaveData:
Select Case Err.Number
Case Else
MsgBox Err.Number & ":" & Err.Description, , "SaveFile 错误提示"
End Select
End Sub