文件传输的难题,请高手解答(附著名的源码)
hcs 2002-11-21 05:39:06 在文件传输时,文件小于4K的情况下,一切正常。但文件大于4K时,有时传输98%,有时传输97%,还有时只传输80%。我快疯了,帮帮我吧!(这是很著名的源代码,很多书上有关文件传输的内容都使用这段代码)
'发送
Private Sub sckSystem_DataArrival(ByVal bytesTotal As Long)
blockSize = 4096
Dim tempComData As String
sckSystem.GetData tempComData, vbString
Dim Command As String
Command = Mid(tempComData, 1, 1)
'MsgBox command
If Command = CANCEL_TRANSFER Then
stopSending
End If
If Command = ENABLE_START Then Command1.Enabled = True
If Command = ACCEPT_TRANSFER Then
DoEvents
sckSend.Connect
Do Until sckSend.State = sckConnected ' Wait until connected
DoEvents
Loop
GoTo BeginTransfer
End If
If Command = CONTINUE_TRANSFER Then
'On Error GoTo ErrorHandler
' Adjust blocksize at end so we don't read too much data
If fileLength - Loc(hIn) <= blockSize Then
blockSize = fileLength - Loc(hIn) + 1
End If
Temp = Space$(blockSize) '// Allocate the read buffer
Get hIn, , Temp '// Read a block of data
ret = DoEvents() '// Check for cancel button event etc.
If quitNow Then GoTo endIt
sckSend.SendData Temp '// Off it goes
'update progress bar
sizeOfFileSent = sizeOfFileSent + blockSize
'On Error GoTo endIt '//
On Error Resume Next
ProgressBar1.value = sizeOfFileSent
compLabel.Caption = sizeOfFileSent & " of " & sizeOfFile & " sent. " & Int(sizeOfFileSent / sizeOfFile * 100) & "%"
DoEvents
If sizeOfFileSent >= sizeOfFile Then
cancel.Caption = "Close"
sckSystem.SendData CLOSE_TRANSFER
End If
Exit Sub
End If
If Command = CLOSE_TRANSFER Then GoTo endIt
Exit Sub
BeginTransfer:
'On Error GoTo ErrorHandler
hIn = FreeFile
Open pathToFile For Binary Access Read As hIn
fileLength = LOF(hIn)
' Adjust blocksize at end so we don't read too much data
If fileLength - Loc(hIn) <= blockSize Then
blockSize = fileLength - Loc(hIn) '+ 1
End If
Temp = Space$(blockSize) '// Allocate the read buffer
Get hIn, , Temp '// Read a block of data
ret = DoEvents() '// Check for cancel button event etc.
If quitNow Then GoTo endIt
sckSend.SendData Temp '// Off it goes
'update progress bar
sizeOfFileSent = sizeOfFileSent + blockSize
'On Error GoTo endIt '//
ProgressBar1.value = sizeOfFileSent
compLabel.Caption = sizeOfFileSent & " of " & sizeOfFile & " sent. " & Int(sizeOfFileSent / sizeOfFile * 100) & "%"
If sizeOfFileSent >= sizeOfFile Then
cancel.Caption = "Close"
sckSystem.SendData CLOSE_TRANSFER
End If
Exit Sub
ErrorHandler: '// Always close the file handle
Close hIn
'SendFile = False
endIt:
'cancel.Caption = "Close"
sckSend.Close 'this severes the data connection, causing the client to save/end the file
Close hIn
MsgBox "Transfer Complete"
Unload Me
End Sub
'接收
Private Sub sckReceive_DataArrival(ByVal bytesTotal As Long)
'On Error GoTo ErrorHandler
Dim TempFileData As String
sckReceive.GetData TempFileData
Put #fileNum, , TempFileData
fileLength = LOF(fileNum)
'update progress bar
'sizeOfFileSent = sizeOfFileSent + bytesTotal
sizeOfFileSent = LOF(fileNum)
On Error GoTo endIt
ProgressBar1.value = sizeOfFileSent
compLabel.Caption = sizeOfFileSent & " of " & sizeOfFile & " sent. " & Int(sizeOfFileSent / sizeOfFile * 100) & "%"
sckSystem.SendData CONTINUE_TRANSFER
DoEvents
Exit Sub
ErrorHandler:
MsgBox "An error occured while saving " & CommonDialog1.FileTitle & ". File Transfer being canceled.", vbOKOnly, "IO Error"
cancel_Click
endIt:
End Sub