文件传输的难题,请高手解答(附著名的源码)
在文件传输时,文件小于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
'发送
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
...全文
相关推荐
国外著名oa系统源码
该源码是国外著名公司开发的oa系统源码 php开发
基于Java语言的LeetCode编程题目解答设计源码
该项目为基于Java语言的LeetCode编程题目解答设计源码,共包含380个文件,其中374个为Java源文件,3个为XML配置文件,1个为Git忽略文件,以及1个Idea项目文件。该源码库旨在帮助开发者通过实践LeetCode题目来提升编程技能。
斯坦福凸优化习题解答[可运行源码]
本资源提供了斯坦福大学经典教材《Convex Optimization》的课后习题详细解答,由Stephen Boyd和L. Vandenberghe编写。该教材是凸优化领域的权威参考,广泛应用于机器学习、信号处理等领域。解答涵盖大部分课后练习题,包含详细解析、解题思路和关键步骤,适合自学者、高校学生和科研人员使用。资源强调先独立解题后参考,鼓励学术交流,同时提醒尊重知识产权。社区维护的解答可能存在不完善之处,欢迎贡献修正。
协议解答源码
YY协议解答源码 解答各种关于YY协议的问题
易语言百科解答源码-易语言
易语言百科解答源码


VB基础类
