Code only executes on Step Through

M

Mike Barrett

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
 
G

Guest

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
 
J

John A. Bailo

John said:
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?)
 
G

Goran Sliskovic

....
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 ....
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
 
M

Mike Barrett

No, it does not look correct to me:


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.

'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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top