Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

ftp response timeout

JamesJames Posts: 217Member
Hi all,
I have a Windows Service that uses ftp to query a folder on a remote server and download any files it finds there, deleting them afterward. It has been running without issue for well over a year until three weeks ago.

Now there is an intermittent error. Periodically when it sends a port command, it hangs waiting for a response from the remote server. According to the admin at the remote server, the command simply never arrives.

This is legacy code from VB.Net 2003 recompiled in VS 2005. Any help or ideas will be greatly appreciated.

-James

[code]
Public Overloads Function DownloadFile(ByVal RemoteFile As String, ByVal LocalFile As String, ByVal StreamMode As StreamModes, ByVal FileMode As DataConnection.FileModes, ByVal AppendFrom As Long) As Boolean
SendCommand("TYPE " + Convert.ToChar(StreamMode))
If Not CreateDataSocket(LocalFile, FileMode, DataConnection.StreamDirections.Download, AppendFrom) Then
RaiseEvent CommandCompleted()
Return False
End If
If Not (passiveTransfers) Then
Dim MyEndPoint As IPEndPoint = dataSocket.GetLocalEndPoint()
SendCommand("PORT " + MyEndPoint.Address.ToString.Replace(".", ",") + "," + CType(Math.Floor(MyEndPoint.Port / 256), Integer).ToString + "," + (MyEndPoint.Port Mod 256).ToString)
End If
If FileMode = DataConnection.FileModes.Append AndAlso AppendFrom > 0 Then
SendCommand("REST " + AppendFrom.ToString)
End If
SendCommand("RETR " + RemoteFile)
If lastResponseType <> 4 AndAlso lastResponseType <> 5 Then
Try
dataSocket.ReceiveFromSocket()
Catch EX As Exception
dataSocket.Close()
WaitForResponse()
RaiseEvent CommandCompleted()
Return False
End Try
WaitForResponse()
End If
dataSocket.Close()
RaiseEvent CommandCompleted()
Return True
End Function

Private Sub SendCommand(Command as String)
Try
clientSocket.Send(ASCII.GetBytes(Command + Convert.ToChar(13) + Convert.ToChar(10)))'ControlChars.CrLf))
If Command.Length >= 4 AndAlso Command.SubString(0, 4).ToUpper.Equals("PASS") Then
RaiseEvent CommandSent("PASS ********")
Else
RaiseEvent CommandSent(Command)
End If
WaitForResponse()
Catch EX As Exception
RaiseEvent ConnectionFailed()
lastResponseNumber = 0
lastResponseType = 0
lastResponse = ""
Throw EX
End Try
End Sub

Private Sub WaitForResponse()
If Not(isNowConnected) Then Exit Sub
Dim tempBuffer(1023) as Byte
Dim retBytes as Integer
Dim retString as String = ""
Do
Try
retBytes = clientSocket.Receive(tempBuffer)
Catch EX As Exception
WriteToMyEventLog(EX.Message & vbCrLf & EX.StackTrace, , EventLogEntryType.Error)
'Throw EX
Exit Do
End Try
retString = retString + ASCII.GetString(tempBuffer, 0, retBytes)
Loop Until IsValidResponse(retString)
lastResponse = retString
If retString.Length >= 3 Then
lastResponseNumber = Short.Parse(retString.Substring(0, 3))
Else
lastResponseNumber = 0
End If
lastResponseType = CType(Math.Floor(lastResponseNumber / 100), Short)
RaiseEvent ReceivedReply(lastResponse, lastResponseNumber, lastResponseType)
End Sub
[/code]

Sign In or Register to comment.