Menu

#7 Bug fixed: receiving messages from slow servers

open
nobody
None
5
2013-12-02
2006-01-18
Anonymous
No

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

Discussion

  • Nobody/Anonymous

    Logged In: NO

    It helped me fix my problem. Thanks a lot.

     
  • Nobody/Anonymous

    Logged In: NO

    I have a similar problem but not sure it is same cause. I
    will deploy Andrea's solution.

    Phil

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.