I used .NET FTP Client in a small project and it
worked fine for local or LAN FTP connections. Slow
connections on Internet often raised an
exception "Index was out of range. Must be non-
negative and less than the size of the collection." in
FTPConnection.GetStream(String remoteFileName, Stream
stream, FTPFileTransferType type).
Debugging the code I found that the private method Read
() raises this exception in the following loop:
while(((string)tempMessage[tempMessage.Count -
1]).Substring(3, 1) == "-")
{
messageList.AddRange(tempMessage);
tempMessage = ReadLines(stream);
}
If ReadLines() returns no message (maybe for
communication slowness) it raises the exception.
I fixed this bug by defining a private method
GetMessage():
private ArrayList GetMessage(NetworkStream stream)
{
ArrayList tempMessage = ReadLines(stream);
int tryCount = 0; while\(tempMessage.Count == 0\) \{ if\(tryCount == 20\) \{ throw new Exception\("Server
does not return message to the message");
}
Thread.Sleep(1000);
tryCount++;
tempMessage = ReadLines(stream);
}
return tempMessage;
}
and modifying the Read() method as follows:
private ArrayList Read ()
{
NetworkStream stream = this.tcpClient.GetStream
();
ArrayList messageList = new ArrayList();
ArrayList tempMessage = GetMessage(stream);
while\(\(\(string\)tempMessage\[tempMessage.Count -
1]).Substring(3, 1) == "-")
{
messageList.AddRange(tempMessage);
tempMessage = GetMessage(stream);
}
messageList.AddRange(tempMessage);
AddMessagesToMessageList\(messageList\); return messageList;
}
I hope this fix can help someone else.
Bye
Andrea
Logged In: NO
It helped me fix my problem. Thanks a lot.
Logged In: NO
I have a similar problem but not sure it is same cause. I
will deploy Andrea's solution.
Phil