issue when using code to FTP file to another server.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • netLynx
    New Member
    • Aug 2007
    • 3

    issue when using code to FTP file to another server.

    I am using the following code to FTP a file from one server to another:

    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
    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:

    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
    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?
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    What is the value of partCount when the error is thrown? I'm betting it's out of bounds. Seeing as how it can be anywhere between -1 and 7 where as your array is only an Integer(5).

    Comment

    • netLynx
      New Member
      • Aug 2007
      • 3

      #3
      PartsCount = 6, but Parts array is Parts(7). Where does 7 come from?

      I guess my issue is, I'm not exactly sure what this line of code is doing:
      Code:
      parts(System.Math.Max(System.Threading.Interlocked.Increment(partCount), partCount - 1)) = Int32.Parse(buf)
      If I step thru in debug mode, ipData has the following in it:
      158,57,168,75,2 2,221 (len=20)
      PartsCount starts at 0 and crashes when it is 6.
      The PARTS array is PARTS(7) which is why it fails. As there are only 6 elements defined, it goes out of bounds, but I don't understand why.
      I guess my question really is what does
      Code:
      System.Math.Max(System.Threading.Interlocked.Increment(partCount), partCount - 1)
      do?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        I don't know where you got parts(7) from. Your parts array is only an integer(5). Look at line 9. If partsCount is 6, you're out of range because your array is only array(5) not 7. I don't know where the 7 you mentioned comes from.

        Comment

        Working...