By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,239 Members | 1,503 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,239 IT Pros & Developers. It's quick & easy.

issue when using code to FTP file to another server.

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

Expand|Select|Wrap|Line Numbers
  1. Private Function createDataSocket() As Socket
  2. sendCommand("PASV")
  3. If retValue <> 227 Then
  4. Throw New IOException(reply.Substring(4))
  5. End If
  6. Dim index1 As Integer = reply.IndexOf("("c)
  7. Dim index2 As Integer = reply.IndexOf(")"c)
  8. Dim ipData As String = reply.Substring(index1 + 1, index2 - index1 - 1)
  9. Dim parts As Integer() = New Integer(5) {}
  10. Dim len As Integer = ipData.Length
  11. Dim partCount As Integer = -1
  12. Dim buf As String = ""
  13. Dim i As Integer = 0
  14. While i < len AndAlso partCount <= 6
  15. Dim ch As Char = [Char].Parse(ipData.Substring(i, 1))
  16. If [Char].IsDigit(ch) Then
  17. buf += ch
  18. ElseIf ch <> ","c Then
  19. Throw New IOException("Malformed PASV reply: " & reply)
  20. End If
  21. If ch = ","c OrElse i + 1 = len Then
  22. Try
  23. parts(System.Math.Max(System.Threading.Interlocked.Increment(partCount), partCount - 1)) = Int32.Parse(buf)
  24. buf = ""
  25. Catch generatedExceptionName As Exception
  26. Throw New IOException("Malformed PASV reply: " & reply)
  27. End Try
  28. End If
  29. i += 1
  30. End While
  31. Dim ipAddress As String = (((CStr(parts(0)) & ".") + CStr(parts(1)) & ".") + CStr(parts(2)) & ".") + CStr(parts(3))
  32. Dim port As Integer = (parts(4) << 8) + parts(5)
  33. Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
  34. Dim ep As New IPEndPoint(Dns.Resolve(ipAddress).AddressList(0), port)
  35. Try
  36. s.Connect(ep)
  37. Catch generatedExceptionName As Exception
  38. Throw New IOException("Can't connect to remote server")
  39. End Try
  40. Return s
  41. End Function
  42.  
  43.  
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:

Expand|Select|Wrap|Line Numbers
  1. Try
  2. parts(System.Math.Max(System.Threading.Interlocked.Increment(partCount), partCount - 1)) = Int32.Parse(buf)
  3. buf = ""
  4. Catch generatedExceptionName As Exception
  5. Throw New IOException("Malformed PASV reply: " & reply)
  6. End Try
  7.  
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?
Jun 10 '13 #1
Share this Question
Share on Google+
3 Replies


Rabbit
Expert Mod 10K+
P: 12,383
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).
Jun 10 '13 #2

P: 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:
Expand|Select|Wrap|Line Numbers
  1. parts(System.Math.Max(System.Threading.Interlocked.Increment(partCount), partCount - 1)) = Int32.Parse(buf)
  2.  
If I step thru in debug mode, ipData has the following in it:
158,57,168,75,22,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
Expand|Select|Wrap|Line Numbers
  1. System.Math.Max(System.Threading.Interlocked.Increment(partCount), partCount - 1)
  2.  
do?
Jun 10 '13 #3

Rabbit
Expert Mod 10K+
P: 12,383
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.
Jun 11 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.