檔案傳遞

檔案傳輸

上面是為了簡便說明!

我在實際傳送時, 由接收端通知來源端從檔案的哪一部份開始傳遞, 由來源端告訴接收端要接收檔案的位置, 緩衝區大小, 及緩衝區內容! 這也是為了避免各封包走不同路徑導致接收時間前後混淆的保護做法, 並且可支援對於未來可能做續傳檔案的部分!

有點多, 可能看得會稍微辛苦些...

摘錄赫密斯部分程式碼

Private Const FileBuffer As Long = 512

Private Enum enuSckFile
 sckFile_OK = 1
 sckFile_Name = 8
 sckFile_Start = 16
 sckFile_End = 17
 sckFile_Continue = 18
 sckFile_Date = 19
 sckFile_Error = 32
End Enum
[略]

Public Function SckSendFile(Socket As Winsock)

Dim sM As enuSckFile ' sckMessage
Dim tBytes(1 To FileBuffer) As Byte
Dim BufferLen As Long
[略]

With Socket
 .GetData sM, vbLong

 Select Case sM
' 待命
 Case Is = sckFile_OK

' 傳送檔名
 Case Is = sckFile_Name
[略]

' 傳檔開始
 Case Is = sckFile_Start
[略]

' 傳檔中
 Case Is = sckFile_Continue
  .GetData tFileSeek, vbLong

  HermesQuickHelp "傳檔中,進度 " + Format((CDbl(tFileSeek) / CDbl(hLen)) * 100, "#.0") + " %"

  If tFileSeek + FileBuffer < hLen Then
   Get #hFile, tFileSeek, tBytes()

   BufferLen = FileBuffer
  Else
   For i = tFileSeek To hLen
    Get #hFile, i, tBytes(i - tFileSeek + 1)
   Next i

   BufferLen = hLen - tFileSeek + 1
  End If

  .SendData sckFile_Continue
  .SendData tFileSeek
  .SendData BufferLen
  .SendData tBytes

' 檔案結束, 傳送建檔時間
 Case Is = sckFile_Date
[略]

' 傳檔結束
 Case Is = sckFile_End
  HermesQuickHelp ""
  Win32MsgBox "傳檔成功!"

  CloseSocket Socket
  Unload Socket

  SckSendFile = True
 End Select
End With

End Function

Public Function SckGetFile(Socket As Winsock)

Dim sM As enuSckFile ' sckMessage
Dim tBytes() As Byte
[略]

With Socket
 .GetData sM, vbLong

 Select Case sM
' 待命
 Case Is = sckFile_OK
  .SendData sckFile_Name

' 接收檔案檔名
 Case Is = sckFile_Name
[略]

' 接收檔案開始
 Case Is = sckFile_Start
[略]

' 接收檔案中
 Case Is = sckFile_Continue
  .GetData tFileSeek, vbLong
  .GetData BufferLen, vbLong
  .GetData tBytes, vbArray + vbByte, FileBuffer

  HermesQuickHelp "接收檔案中,進度 " + Format((CDbl(tFileSeek + BufferLen) / CDbl(hLen)) * 100, "#.0") + " %"

  If tFileSeek + BufferLen < hLen Then
   Put #hFile, tFileSeek, tBytes()
   .SendData sckFile_Continue
   .SendData CLng(Seek(hFile))
  Else
   lb = LBound(tBytes)
   ub = UBound(tBytes)

   For i = 1 To BufferLen
    Put #hFile, tFileSeek + i - 1, tBytes(i - 1 + lb)
   Next i

   Close hFile
   .SendData sckFile_Date

  End If

' 檔案結束, 傳送建檔時間
 Case Is = sckFile_Date
[略]

' 接收檔案結束
 Case Is = sckFile_End
[略]

 End Select
End With

End Function
[略]

 

 

註 1:本網頁整理中,若有說明不清,請忍耐至本網頁整理完畢,若有建議,請逕行 E-Mail 給我