VB.NET

Moderators: seancampbell
Number of threads: 4020
Number of posts: 10026

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
ftp response timeout Posted by James on 4 Jan 2011 at 2:31 PM
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

    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





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.