Home > Software engineering >  WINSOCK implementation FTP download, normal for the first time, the second time?
WINSOCK implementation FTP download, normal for the first time, the second time?

Time:01-16

I made a WINSOCK FTP download tools, each run, for the first time will be able to download, but after the download is complete, the download will not be able to connect again, but shut the process down to open the can, excuse me what reason be cause? Thank you

The code is as follows:
Option Explicit

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private wsFtpServerIP As String
Private wsFtpServerPort As String
Private wsFtpUserName As String
Private wsFtpPassword As String

Private wsFtpLocalFile As String
Private wsFtpLocalFileLen As the Variant
Private wsFtpLocalFileMaxLen As the Variant
Private wsFtpLocalNum As Long
Private wsFtpHostFile As String

Private wsGetDataStr As String

'whether to open the Winsock for download
The Public Function StartWinsock (ByVal tConnectWs As Winsock, ByVal tDownloadWs As Winsock, ByVal tHostFile As String, ByVal tLocalFile As String, ByVal tFtpIp As String, Optional ByVal tPort As String="21", Optional ByVal tUserName As String="anonymous", Optional ByVal tPw As String) As Boolean

Dim PathT As String
Dim FileT As String
Dim FileHaveLen As String
Dim tSplit () As String

Dim tStart As Boolean

'initialize content
WsFtpServerIP=tFtpIp
WsFtpServerPort=tPort
WsFtpUserName=tUserName
WsFtpPassword=tPw
WsFtpHostFile=tHostFile
WsFtpLocalFile=tLocalFile

With tConnectWs
If. The State=0 Then disconnect rewiring
'The RemoteHost=wsFtpServerIP
The RemotePort=wsFtpServerPort
. Connect
Do
If. State=7 Then Exit the Do
DoEvents
Loop
End the If
. SendData publishes the event "the USER" & amp; "Anonymous" & amp; VbCrLf 'enter account

The Debug. Print "# # # # # # # # # # # # # # # # # # # # # # # #" & amp; The State
Do
If the State=7 Then
'If Len (wsGetDataStr) & lt;=0 Then
'SendData publishes the event "the USER" & amp; "Anonymous" & amp; VbCrLf 'enter account
'the Else
TSplit ()=Split (wsGetDataStr, Space (1), and vbTextCompare)
The Select Case LCase (Left (wsGetDataStr, 3))

Case "220"
. SendData publishes the event "the USER" & amp; "Anonymous" & amp; VbCrLf 'enter account
WsGetDataStr=""

Case "331"
. SendData publishes the event "PASS" & amp; "" & amp; VbCrLf 'input password
WsGetDataStr=""

Case "230"
. SendData publishes the event "TYPE I" & amp; VbCrLf 'connection success
WsGetDataStr=""

Case "530"
. Close
WsGetDataStr=""

'the following
for connection after a successful operation
Case "200"
TConnectWs. SendData publishes the event "PASV" & amp; VbCrLf 'modification for PASV mode
WsGetDataStr=""

Case "227"
'get temporary port and IP
Dim Tmp1, Tmp2, Tmp3 Tmp4
Dim TmpIp, TmpPort

Tmp1=InStr (wsGetDataStr, CRH (40)) + 1
Tmp2=InStrRev (wsGetDataStr, CRH (41))
Tmp3=Mid (wsGetDataStr, Tmp1, Tmp2 - Tmp1)
Tmp4=Split (Tmp3, ", ")
TmpIp=Tmp4 (0) & amp; ". "& amp; Tmp4 (1) & amp; ". "& amp; Tmp4 (2) & amp; ". "& amp; Tmp4 (3)
TmpPort=Tmp4 (4) * 256 + Tmp4 (5)

. SendData publishes the event "PWD" & amp; VbCrLf
WsGetDataStr=""

Case "257" 'with PWD to return to the current directory
WsFtpHostFile=Replace (wsFtpHostFile, "", "/")
PathT=Left (wsFtpHostFile InStrRev (wsFtpHostFile, "/"))
'change directory
If tSplit (1)="/" Then "If it is the root directory
. SendData publishes the event "CWD" & amp; PathT & amp; VbCrLf
The Else
CWD. SendData publishes the event "/" & amp; PathT & amp; VbCrLf
End the If
WsGetDataStr=""

Case "250"
Get the file size
The Debug. Print "wsFtpHostFile=" & amp; WsFtpHostFile
FileT=Right (wsFtpHostFile, Len (wsFtpHostFile) - InStrRev (wsFtpHostFile, "/"))
TConnectWs. SendData publishes the event "SIZE" & amp; FileT & amp; VbCrLf
WsGetDataStr=""

Case "213"
'written to the file
The Debug. Print "Len (wsGetDataStr)=" & amp; Len (wsGetDataStr)
TSplit ()=Split (wsGetDataStr, "",, vbTextCompare)
WsFtpLocalFileMaxLen=Val (Replace (tSplit (1), vbCrLf, "",,, vbTextCompare))

WsFtpLocalNum=IIf (wsFtpLocalNum=0, FreeFile (), wsFtpLocalNum)
The Open wsFtpLocalFile For Binary Lock Write As # wsFtpLocalNum
If LOF (wsFtpLocalNum) & gt; 0 Then
FileHaveLen=FileLen (wsFtpLocalFile)
Close # wsFtpLocalNum
The Else
Close # wsFtpLocalNum
End the If
TConnectWs. SendData publishes the event "REST" & amp; FileHaveLen & amp; VbCrLf 'cannot be removed, or file will continue to write

'data download
With tDownloadWs
If TmpIp & lt;> The RemoteHost Then
The RemoteHost=TmpIp
The RemotePort=TmpPort
If the State=7 Then. Close
. Connect
End the If
End With
TConnectWs. SendData publishes the event "RETR" & amp; FileT & amp; VbCrLf
TStart=True

WsFtpLocalNum=IIf (wsFtpLocalNum=0, FreeFile (), wsFtpLocalNum)
The Open wsFtpLocalFile For Binary Lock Write As # wsFtpLocalNum
WsGetDataStr=""

Case "226"
Close # wsFtpLocalNum
'SendData publishes the event "QUIT" & amp; VbCrLf
WsGetDataStr=""
The Exit Do
End the Select
'wsGetDataStr=""
The Else

If LCase (Left (wsGetDataStr, 3)="221" Then 'formal disconnect
'If tStart=True Then
'StartWinsock=True
WsFtpLocalNum=0
. Close
TDownloadWs. Close
The Exit Do
'End If
End the If
End the If
DoEvents
Loop
MsgBox "FFF
"End With

End the Function




'events connected WINSOCK in DataArrival
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related