vb.net ftp sub times out before file finishes uploading

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im pretty sure some of this may have been covered before, but my comprehension of what was said is poor. So Im just going to ask for help. Im really hoping there is an easy solution to my issue.
Ill go ahead and post all the code I am dealing with [usernames/passwords/serveraddress has cleaned]
Ok the codePublic Function CheckIfFtpFileExists(ByVal fileUri As String) As Boolean
Dim request As FtpWebRequest = WebRequest.Create(fileUri)
request.Credentials = New NetworkCredential("MyUserName", "MyPassword")
request.Method = WebRequestMethods.Ftp.GetFileSize
Try
Dim response As FtpWebResponse = request.GetResponse()
THE FILE EXISTS
Catch ex As WebException
Dim response As FtpWebResponse = ex.Response
If FtpStatusCode.ActionNotTakenFileUnavailable = response.StatusCode Then
THE FILE DOES NOT EXIST
Return False
End If
End Try
Return True
End Function <--- works like a charm!


Public Sub DoMirror()
DrvLtr = DrvSelect.Text
RamDrvLtr = RamDrvSelect.Text
TempDrvLtr = TempDrvSelect.Text
SkipBuildPhase = CheckBox8.CheckState

Dim Location As String

DrvLtr = "D:"
RamDrvLtr = "L"
TempDrvLtr = "N"

RichTextBox1.AppendText("Starting Run" & Chr(13))
Try
WshShell = CreateObject("WScript.Shell")
WshShell.CurrentDirectory = RamDrvLtr & "xbmc.reposWorkingRepoTCG-Repo"
RichTextBox1.AppendText("Copying and sorting folders" & Chr(13))
For Each Dir As String In Directory.GetDirectories(".")
CountStart = CountStart + 1
Debug.WriteLine(Dir)
Dim DirName = Replace(Dir, ".", "").ToString()
RichTextBox1.AppendText("Scanning " & DirName & " to BuildPhase" & Chr(13))

WshShell = CreateObject("WScript.Shell")
WshShell.CurrentDirectory = RamDrvLtr & "xbmc.reposWorkingRepoTCG-Repo" & DirName
For Each SubDir As String In Directory.GetDirectories(".")
CountStart = CountStart + 1
Debug.WriteLine(SubDir)
Dim SubDirName = Replace(SubDir, ".", "").ToString()
RichTextBox1.AppendText("Scanning " & SubDir & " to BuildPhase" & Chr(13))


WshShell = CreateObject("WScript.Shell")
WshShell.CurrentDirectory = RamDrvLtr & "xbmc.reposWorkingRepoTCG-Repo" & DirName & "" & SubDirName
Debug.WriteLine(RamDrvLtr & "xbmc.reposWorkingRepoTCG-Repo" & DirName & "" & SubDirName)

Dim di As New IO.DirectoryInfo(RamDrvLtr & "xbmc.reposWorkingRepoTCG-Repo" & DirName & "" & SubDirName)
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo

For Each dra In diar1
RichTextBox1.AppendText(dra.ToString() & Chr(13))

alrighty then

Location = "ftp://MYFTP.com/xbmc/binaries/TCG-Repo/" & DirName & "/" & SubDirName & "/" & dra.ToString() < ---- For now for testing.

If CheckIfFtpFileExists(Location) = False Then

RichTextBox1.AppendText("Uploading " & dra.ToString() & Chr(13))
Dim FileName As String = RamDrvLtr & "xbmc.reposWorkingRepoTCG-Repo" & DirName & "" & SubDirName & "" & dra.ToString()

Debug.WriteLine(Location & "wasnt there so uploadit")

UploadFile(FileName, Location)

Else
RichTextBox1.AppendText(dra.ToString() & " is already there" & Chr(13))
End If
Next
Next
Next

Catch ex As Exception
RichTextBox2.Text = RichTextBox2.Text & "!!!!!!! ------------------------------------------------ !!!!!!" & NL
RichTextBox2.Text = RichTextBox2.Text & "There was an error @ DoMirror " & ex.Message.ToString() & NL
RichTextBox2.Text = RichTextBox2.Text & "!!!!!!! ------------------------------------------------ !!!!!!" & NL
End Try
WshShell = Nothing
End Sub

Public Sub UploadFile(ByVal File, ByVal Location)
set up request...
Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(Location), System.Net.FtpWebRequest)
clsRequest.Credentials = New System.Net.NetworkCredential("MyUserName", "MyPassword")
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

read in file...
Dim bFile() As Byte = System.IO.File.ReadAllBytes(File)

upload file...
Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()
End Sub
Some sample output I see below
skin.MediaStream-3692.zip
skin.MediaStream-3692.zip is already there
skin.MiniMeedia-3692.zip
skin.MiniMeedia-3692.zip is already there
skin.Xbox-Classic-3692.zip
skin.Xbox-Classic-3692.zip is already there
skin.Connected-3692.zip
skin.Connected-3692.zip is already there
skin.T9-3692.zip
Uploading skin.T9-3692.zip <--worked @ 403k
skin.Visor-3692.zip
Uploading skin.Visor-3692.zip <--worked @ 3.9M
skin.Alaska-111.zip
Uploading skin.Alaska-111.zip errors at about 5 megs of 110M

It works fine on small(ish) files [say under 5 megs] but on larger files it times out or errors out or whatever with this

"There was an error @ DoMirror The underlying connection was closed: An unexpected error occurred on a receive."

That is what my try catch returns, there is no errro in the output console in VS

The file continues to upload and finish but the sub borks in the loop.

Are loops set to a predetirmed amount of time?
Does .net set a timeout value for Imports System.Net?

Can I set a finally to restart the loop on error since the file DOES get uploaded so I can just rescan and it will automatically go to the next file that is not there?

Please use easy words :) and since I cant even do this in VB answering with C# would just confuse me further.
Thanks

View the full article
 
Back
Top