1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
|
FTP.vb
Const BUFFSIZE As Integer = 4096
Friend Function ReceiveFile(ByVal sLocalFilename As String, ByVal sRemoteFilename As String, ByVal XferMode As TransferMode) As Boolean
Dim objLocalFileStream As FileStream
Dim mTCPData As New TcpClient
Dim mDataStream As NetworkStream
Dim Port As Integer = 20
Dim strIPAddress As String
Dim sOut As String = ""
If (Not bConnectionOpen) Then
Throw New FtpClientException(0, "ReceiveFile" & vbCrLf & "Connection not open")
End If
Try
objLocalFileStream = New FileStream(sLocalFilename, FileMode.Create, FileAccess.ReadWrite, FileShare.Read, BUFFSIZE, False)
Catch ex As FileNotFoundException
Throw New FtpClientException(0, "Open Local File - File Not Found(" & vbCrLf & sLocalFilename & vbCrLf & ex.Message)
Catch ex As DirectoryNotFoundException
Throw New FtpClientException(0, "Open Local File - Directory Not Found" & vbCrLf & sLocalFilename & vbCrLf & ex.Message)
Catch ex As SecurityException
Throw New FtpClientException(0, "Open Local File" & vbCrLf & sLocalFilename & vbCrLf & ex.Message)
Catch ex As UnauthorizedAccessException
Throw New FtpClientException(0, "Open Local File" & vbCrLf & sLocalFilename & vbCrLf & ex.Message)
Catch ex As Exception
Throw New FtpClientException(0, "Open Local File" & vbCrLf & sLocalFilename & vbCrLf & ex.Message)
End Try
' Set transfer mode
Select Case XferMode
Case TransferMode.Ascii
SendFTPCommand("TYPE A")
sOut = ReadReply()
Case TransferMode.Binary
SendFTPCommand("TYPE I")
sOut = ReadReply()
End Select
Application.DoEvents()
'
'
Call ReadyDataSocketAndSendCommand("RETR " & Path.GetFileName(sRemoteFilename), "ReceiveFile", mTCPData, mDataStream)
Dim bData(8192) As Byte
Dim bytesRead As Integer = 0
' Retrieve the file
Try
Do
bytesRead = mDataStream.Read(bData, 0, bData.Length)
objLocalFileStream.Write(bData, 0, bytesRead)
Application.DoEvents()
Loop While mDataStream.DataAvailable
'Originally recieved size errors here with regards to the network stream,
'method rewritten to above in order to resolve issue. BUFFSIZE was smaller
'than bData.
'bytesRead = mDataStream.Read(bData, 0, BUFFSIZE)
'Do While (bytesRead > 0)
' objLocalFileStream.Write(bData, 0, bytesRead)
' bytesRead = mDataStream.Read(bData, 0, BUFFSIZE)
' Application.DoEvents()
'Loop
Catch ex As Exception
DBGeneralError("FTP.ReceiveFile", Err.GetException())
objLocalFileStream.Close()
objLocalFileStream = Nothing
mDataStream.Close()
mDataStream = Nothing
mTCPData.Close()
mTCPData = Nothing
Thread.Sleep(400)
sOut = ReadReply()
'If we have gotten here, we have failed.
Return False
End Try
objLocalFileStream.Close()
objLocalFileStream = Nothing
mDataStream.Close()
mDataStream = Nothing
mTCPData.Close()
mTCPData = Nothing
Thread.Sleep(400)
sOut = ReadReply()
'If we have gotten here, we should be good.
Return True
End Function
Sub ReadyDataSocketAndSendCommand(ByVal strCommand As String, ByVal strMethodName As String, ByRef mTCPData As TcpClient, ByRef mDataStream As NetworkStream)
Dim sOut As String
Dim strIPAddress As String
If (Not bConnectionOpen) Then
Throw New FtpClientException(0, strMethodName & vbCrLf & "Connection not open")
End If
'
' Set Passive Mode
'
' Passive mode opens the connection on the remote computer and returns()
' a port number to use. Later, this causes message 125. No worries!()
' That's what is supposed to happen.
'
SendFTPCommand("PASV")
sOut = ReadReply()
If Not ReplyContains("227", sOut, strErrorCode, strErrorMessage) Then
Throw New FtpClientException(CInt(strErrorCode), "PASV" & vbCrLf & strErrorMessage)
End If
ParsePASVResult(sOut, strIPAddress, Port)
Application.DoEvents()
'
' Open a socket
'
Try
mTCPData = New TcpClient(strIPAddress, Port)
Catch ex As Exception
Throw New FtpClientException(0, "Open Socket" & vbCrLf & strIPAddress & " " & Port.ToString & vbCrLf & ex.Message)
End Try
mTCPData.ReceiveBufferSize = BUFFSIZE
mTCPData.SendBufferSize = BUFFSIZE
Try
mDataStream = mTCPData.GetStream()
Catch ex As Exception
Throw New FtpClientException(0, "GetStream" & vbCrLf & strIPAddress & " " & Port.ToString & vbCrLf & ex.Message)
End Try
' Send the FTP Command to the FTP Server
SendFTPCommand(strCommand)
sOut = ReadReply()
' We will get either a confirmation of the download or an error Message()
If Not ReplyContains("150", sOut, strErrorCode, strErrorMessage) AndAlso Not ReplyContains("125", sOut, strErrorCode, strErrorMessage) Then
Throw New FtpClientException(CInt(strErrorCode), strCommand & vbCrLf & strErrorMessage)
End If
End Sub
|