代码:
Option ExplicitDim MyID As Long
Dim FileNum As Long
Dim FileName As String
Dim RCVAccept As Boolean
Dim Sentbyt As Long
Dim ByteSec As Long, Speed As Long
Dim Complete As BooleanPublic Function InitTransfer(ByVal ID As Long)
MyID = ID
FileName = Mid(ftSend(MyID).FileToSend, InStrRev(ftSend(MyID).FileToSend, "\") + 1)
Caption = "Sending file to " & ftSend(MyID).To
lblInfo = FileName & " to " & ftSend(MyID).To
'Attempt to connect to the Destination
wsSend.Connect ftSend(MyID).To, FT_USE_PORT
Me.Visible = True
End FunctionPrivate Sub cmdCancel_Click()
On Error Resume Next
Complete = True
Close #FileNum
If chkClose.Value = vbUnchecked Then Unload Me
End SubPrivate Sub cmdCancelClose_Click()
On Error Resume Next
'Close the connection to stop
Complete = True
wsSend.Close
Close #FileNum
Unload Me
End SubPrivate Sub Form_Unload(Cancel As Integer)
'Remove the form from memory
Set ftSend(MyID).frmSend = Nothing
End SubPrivate Sub tmrSpeed_Timer()
Speed = Format(ByteSec / 1024, "0.0")
ByteSec = 0
End SubPrivate Sub wsSend_Close()
On Error Resume Next
If Not Complete Then
MsgBox "File Transfer Ended Unexpectedly!", vbCritical + vbOKOnly, "Error"
Close #FileNum
Unload Me
End If
End SubPrivate Sub wsSend_Connect()
'Send Information regarding the file
wsSend.SendData "FILE:" & FileName & ":" & ftSend(MyID).FileSize & ":" & ftSend(MyID).Comment
End SubPrivate Sub wsSend_DataArrival(ByVal bytesTotal As Long) Dim Dat As String
wsSend.GetData Dat, vbString
If Trim$(Dat$) = "ACCEPT" Then
Call SendChunk
ElseIf Trim$(Dat$) = "DENIED" Then
MsgBox "文件被拒绝!", vbInformation + vbOKOnly, "提示:"
'Close the connection
wsSend.Close
'unload the form
Unload Me
End If
End SubPrivate Sub wsSend_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Select Case Number
Case sckConnectionRefused, sckHostNotFound, sckHostNotFoundTryAgain
'couldnt connect
MsgBox "Could Not Connect To Remote Host!", vbCritical + vbOKOnly, _
"Error " & Number
'Close the form
Unload Me
End Select
End SubPublic Function SendChunk()
'This is where we send the file data
Dim ChunkSize As Long
Dim Chunk() As Byte
Dim arrHash() As Byte
If wsSend.State <> sckConnected Then Exit Function
ChunkSize = FT_BUFFER_SIZE
If FileNum = 0 Then 'No data has been sent yet, open the file
FileNum = FreeFile
Open ftSend(MyID).FileToSend For Binary As #FileNum
End If
'determine chunk size
If (LOF(FileNum) - Loc(FileNum)) < FT_BUFFER_SIZE Then _
ChunkSize = (LOF(FileNum) - Loc(FileNum))
'set array size to fit chunk
ReDim Chunk(0 To ChunkSize - 1)
'read the chunk
Get #FileNum, , Chunk
'Send the data
wsSend.SendData Chunk
Sentbyt = Sentbyt + ChunkSize
ByteSec = ByteSec + ChunkSize
pgPercent.Value = (100 / ftSend(MyID).FileSize) * Sentbyt
lblSent = "Sent " & Int(pgPercent.Value) & "% of " & ftSend(MyID).FileSize / 1024 & _
"Kb @ " & Speed & " Kb\Sec"
'See if file is sent
If Sentbyt = ftSend(MyID).FileSize Then
Complete = True
Close #FileNum
cmdCancelClose.Caption = "&Close"
End If
End FunctionPrivate Sub wsSend_SendComplete()
DoEvents
If FileNum > 0 Then
If Not Complete Then
SendChunk
Else
If chkClose.Value = Checked Then
wsSend.Close
Unload Me
End If
End If
End If
End Sub
错误如下: lblSent = "Sent " & Int(pgPercent.Value) & "% of " & ftSend(MyID).FileSize / 1024 & _
"Kb @ " & Speed & " Kb\Sec"下标越界 实时错误9
Option ExplicitDim MyID As Long
Dim FileNum As Long
Dim FileName As String
Dim RCVAccept As Boolean
Dim Sentbyt As Long
Dim ByteSec As Long, Speed As Long
Dim Complete As BooleanPublic Function InitTransfer(ByVal ID As Long)
MyID = ID
FileName = Mid(ftSend(MyID).FileToSend, InStrRev(ftSend(MyID).FileToSend, "\") + 1)
Caption = "Sending file to " & ftSend(MyID).To
lblInfo = FileName & " to " & ftSend(MyID).To
'Attempt to connect to the Destination
wsSend.Connect ftSend(MyID).To, FT_USE_PORT
Me.Visible = True
End FunctionPrivate Sub cmdCancel_Click()
On Error Resume Next
Complete = True
Close #FileNum
If chkClose.Value = vbUnchecked Then Unload Me
End SubPrivate Sub cmdCancelClose_Click()
On Error Resume Next
'Close the connection to stop
Complete = True
wsSend.Close
Close #FileNum
Unload Me
End SubPrivate Sub Form_Unload(Cancel As Integer)
'Remove the form from memory
Set ftSend(MyID).frmSend = Nothing
End SubPrivate Sub tmrSpeed_Timer()
Speed = Format(ByteSec / 1024, "0.0")
ByteSec = 0
End SubPrivate Sub wsSend_Close()
On Error Resume Next
If Not Complete Then
MsgBox "File Transfer Ended Unexpectedly!", vbCritical + vbOKOnly, "Error"
Close #FileNum
Unload Me
End If
End SubPrivate Sub wsSend_Connect()
'Send Information regarding the file
wsSend.SendData "FILE:" & FileName & ":" & ftSend(MyID).FileSize & ":" & ftSend(MyID).Comment
End SubPrivate Sub wsSend_DataArrival(ByVal bytesTotal As Long) Dim Dat As String
wsSend.GetData Dat, vbString
If Trim$(Dat$) = "ACCEPT" Then
Call SendChunk
ElseIf Trim$(Dat$) = "DENIED" Then
MsgBox "文件被拒绝!", vbInformation + vbOKOnly, "提示:"
'Close the connection
wsSend.Close
'unload the form
Unload Me
End If
End SubPrivate Sub wsSend_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Select Case Number
Case sckConnectionRefused, sckHostNotFound, sckHostNotFoundTryAgain
'couldnt connect
MsgBox "Could Not Connect To Remote Host!", vbCritical + vbOKOnly, _
"Error " & Number
'Close the form
Unload Me
End Select
End SubPublic Function SendChunk()
'This is where we send the file data
Dim ChunkSize As Long
Dim Chunk() As Byte
Dim arrHash() As Byte
If wsSend.State <> sckConnected Then Exit Function
ChunkSize = FT_BUFFER_SIZE
If FileNum = 0 Then 'No data has been sent yet, open the file
FileNum = FreeFile
Open ftSend(MyID).FileToSend For Binary As #FileNum
End If
'determine chunk size
If (LOF(FileNum) - Loc(FileNum)) < FT_BUFFER_SIZE Then _
ChunkSize = (LOF(FileNum) - Loc(FileNum))
'set array size to fit chunk
ReDim Chunk(0 To ChunkSize - 1)
'read the chunk
Get #FileNum, , Chunk
'Send the data
wsSend.SendData Chunk
Sentbyt = Sentbyt + ChunkSize
ByteSec = ByteSec + ChunkSize
pgPercent.Value = (100 / ftSend(MyID).FileSize) * Sentbyt
lblSent = "Sent " & Int(pgPercent.Value) & "% of " & ftSend(MyID).FileSize / 1024 & _
"Kb @ " & Speed & " Kb\Sec"
'See if file is sent
If Sentbyt = ftSend(MyID).FileSize Then
Complete = True
Close #FileNum
cmdCancelClose.Caption = "&Close"
End If
End FunctionPrivate Sub wsSend_SendComplete()
DoEvents
If FileNum > 0 Then
If Not Complete Then
SendChunk
Else
If chkClose.Value = Checked Then
wsSend.Close
Unload Me
End If
End If
End If
End Sub
错误如下: lblSent = "Sent " & Int(pgPercent.Value) & "% of " & ftSend(MyID).FileSize / 1024 & _
"Kb @ " & Speed & " Kb\Sec"下标越界 实时错误9
这个程序是一个网络程序,双方开启这个程序就可以传输文件,但是我发现大约500K以上的文件
传到一次数量就出现下标越界.....很苦恼