I am using the following code to FTP a file from one server to another:
the connections are all OK and I verified that the ID and password are fine, however, in this TRY...CATCH block, I get an out of bounds error:
I'm not sure I understand why. All I have is a simple text file, that has "How are you?" in it. I want to test this code by FTPing this test.txt file from my server to another server.
Can you assist?
Code:
Private Function createDataSocket() As Socket
sendCommand("PASV")
If retValue <> 227 Then
Throw New IOException(reply.Substring(4))
End If
Dim index1 As Integer = reply.IndexOf("("c)
Dim index2 As Integer = reply.IndexOf(")"c)
Dim ipData As String = reply.Substring(index1 + 1, index2 - index1 - 1)
Dim parts As Integer() = New Integer(5) {}
Dim len As Integer = ipData.Length
Dim partCount As Integer = -1
Dim buf As String = ""
Dim i As Integer = 0
While i < len AndAlso partCount <= 6
Dim ch As Char = [Char].Parse(ipData.Substring(i, 1))
If [Char].IsDigit(ch) Then
buf += ch
ElseIf ch <> ","c Then
Throw New IOException("Malformed PASV reply: " & reply)
End If
If ch = ","c OrElse i + 1 = len Then
Try
parts(System.Math.Max(System.Threading.Interlocked.Increment(partCount), partCount - 1)) = Int32.Parse(buf)
buf = ""
Catch generatedExceptionName As Exception
Throw New IOException("Malformed PASV reply: " & reply)
End Try
End If
i += 1
End While
Dim ipAddress As String = (((CStr(parts(0)) & ".") + CStr(parts(1)) & ".") + CStr(parts(2)) & ".") + CStr(parts(3))
Dim port As Integer = (parts(4) << 8) + parts(5)
Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim ep As New IPEndPoint(Dns.Resolve(ipAddress).AddressList(0), port)
Try
s.Connect(ep)
Catch generatedExceptionName As Exception
Throw New IOException("Can't connect to remote server")
End Try
Return s
End Function
Code:
Try
parts(System.Math.Max(System.Threading.Interlocked.Increment(partCount), partCount - 1)) = Int32.Parse(buf)
buf = ""
Catch generatedExceptionName As Exception
Throw New IOException("Malformed PASV reply: " & reply)
End Try
Can you assist?
Comment