I am using VS 2005, VB.Net. I need to connect to an FTP server and get a
list of files. I plundered the following code from MSDN to accomplish this
task. It was part of a class called clsFTP:
Public Function GetFileList(ByVal sMask As String) As String()
Dim cSocket As Socket
Dim bytes As Int32
Dim seperator As Char = ControlChars.Lf
Dim mess() As String
m_sMes = ""
'Check if you are logged on to the FTP server.
If (Not (m_bLoggedIn)) Then
Login()
End If
cSocket = CreateDataSocket()
'Send an FTP command,
SendCommand("NLST " & sMask)
If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
m_sMes = ""
Do While (True)
Array.Clear(m_aBuffer, 0, m_aBuffer.Length)
bytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)
m_sMes += ASCII.GetString(m_aBuffer, 0, bytes)
' Console.WriteLine(m_sMes)
If (bytes < m_aBuffer.Length) Then
Exit Do
End If
Loop
mess = m_sMes.Split(seperator)
cSocket.Close()
ReadReply()
If (m_iRetValue <> 226) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
Return mess
End Function
I connect to the FTP server perfectly. The rest of the functions in that
class work great, such as download, upload, etc.. Sadly, GetFileList only
returns the first file unless I step through, then it returns everything I
have. If I put a break point at the Do While line, then run again, it
executes perfectly. If I let it run, it gives me just the first file. The
mask I am using is also correct.
Any clues??? It is driving me insane. Does the code look tight? I have
tried everything I know how.
Thanks,
Mike
needhelp - 15 Dec 2005 14:51 GMT
I cannot post questions as "new question".
I am installing IIS on my win xp professional. However, there is no IIS
listed in the "add/remove windows components". Anybody can help?
Thanks
Armin Zingler - 15 Dec 2005 14:59 GMT
> I cannot post questions as "new question".
Use a newsreader like outlook express.
Armin
John Bailo - 15 Dec 2005 15:23 GMT
XP doesn't support IIS.
Get a copy of Apache/Tomcat.
> I cannot post questions as "new question".
>
> I am installing IIS on my win xp professional. However, there is no IIS
> listed in the "add/remove windows components". Anybody can help?
>
> Thanks
John A. Bailo - 15 Dec 2005 17:41 GMT
> XP doesn't support IIS.
>
> Get a copy of Apache/Tomcat.
Oppps.
Correction.
XP Home doesn't support IIS.
Get a copy of Apache/Tomcat.
(Are you sure you have XP pro? Do you have the CD?)
>> I cannot post questions as "new question".
>> I am installing IIS on my win xp professional. However, there is no
>> IIS listed in the "add/remove windows components". Anybody can help?
>>
>> Thanks
Goran Sliskovic - 15 Dec 2005 20:06 GMT
...
> m_sMes = ""
> Do While (True)
[quoted text clipped - 6 lines]
> End If
> Loop
...
> I connect to the FTP server perfectly. The rest of the functions in that
> class work great, such as download, upload, etc.. Sadly, GetFileList only
> returns the first file unless I step through, then it returns everything I
> have. If I put a break point at the Do While line, then run again, it
> executes perfectly. If I let it run, it gives me just the first file. The
> mask I am using is also correct.
...
No, it does not look correct to me:
> If (bytes < m_aBuffer.Length) Then
> Exit Do
> End If
This is not good criteria to leave loop as cSocket.Receive does not have to
return exact number of bytes that was requested. It can return less. It will
return number of bytes currently available (buffered by the OS). In case you
run it in debug mode and stop in breakpoint, you are giving network some
time to transfer more bytes then m_aBuffer can hold, so Receive will fill
whole buffer.
Regards,
Goran
Mike Barrett - 15 Dec 2005 20:32 GMT
> No, it does not look correct to me:
>
[quoted text clipped - 8 lines]
> network some time to transfer more bytes then m_aBuffer can hold, so
> Receive will fill whole buffer.
'Do While (True)
Array.Clear(m_aBuffer, 0, m_aBuffer.Length)
bytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)
m_sMes += ASCII.GetString(m_aBuffer, 0, bytes)
' Console.WriteLine(m_sMes)
'If (bytes < m_aBuffer.Length) Then
'Exit Do
'End If
'Loop
Commenting out the loop returned a perfect value.