我写的一个SLOWWWWW FTP订阅组需要一些帮助. [英] Need some help with a SLOWWWWW group of FTP subs i wrote.

查看:47
本文介绍了我写的一个SLOWWWWW FTP订阅组需要一些帮助.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个程序,该程序通过SQL和FTP将其数据同步到我们公司的服务器.我编写了以下代码,并将它们放在一个模块中,以方便进行FTP编码,它们可以正常工作,但对于仅上传几个Mb的数据却非常慢,而且我认为问题出在这里的不是大文件,而是大量文件和因此建立了比必要更多的FTP连接.我试图尽可能减少这种情况,但是在更深的递归目录上它似乎总是很慢....

Imports System.IO 

Public Module Sync

    Public Sub UploadFile(ByVal Game As String, ByVal Path As String, ByVal FileName As String, ByVal Source As String, ByVal User As String)

        Path = Path.Trim("/")

        If Not Path = "" Then

            MakeDir(Game, Path, User)

        End If

        Dim sr = New StreamReader(Source, System.Text.Encoding.Default)

        Dim RawFile As String = sr.ReadToEnd

        sr.Close()

        For i = 1 To 5

            Try

                Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://server.net/" + User + "/Profiles/" + Game + "/GameData/" + Path + "/" + FileName), System.Net.FtpWebRequest)

                clsRequest.Credentials = New System.Net.NetworkCredential("user", "pass")

                clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

                Dim bFile = System.Text.Encoding.Default.GetBytes(RawFile)

                Dim clsStream = _

                    clsRequest.GetRequestStream()

                clsStream.Write(bFile, 0, bFile.Length)

                clsStream.Close()

                clsStream.Dispose()

                Exit For

            Catch err As Exception

                If i = 5 Then

                    MsgBox("Unable to upload """ & Source & """ to """ & "ftp://dsdcomputers.net/" + User + "/Profiles/" + Game + "/GameData/" + Path + "/" + FileName & """ after 5 attempts because of the following error:" & vbCrLf & vbCrLf & err.ToString)

                End If

            End Try

        Next

    End Sub

    Public Sub MakeDir(ByVal Game As String, ByVal Path As String, ByVal User As String)

        Path = Path.Trim("/")

        If Path = Nothing Then

            Exit Sub

        End If

        If Path.Contains("/") Then

            Dim PathArray As String() = Path.Split("/")

            Path = ""

            For Each folder In PathArray

                Path = Path & "/" & folder

                Try

                    Path = Path.Trim("/")

                    Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://server.net/" + User + "/Profiles/" + Game + "/GameData/" + Path), System.Net.FtpWebRequest)

                    clsRequest.Credentials = New System.Net.NetworkCredential("user", "pass")

                    clsRequest.Method = System.Net.WebRequestMethods.Ftp.MakeDirectory

                    Dim response = clsRequest.GetResponse()

                Catch err As Exception

                End Try

            Next

        Else

            Try

                Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://server.net/" + User + "/Profiles/" + Game + "/GameData/" + Path), System.Net.FtpWebRequest)

                clsRequest.Credentials = New System.Net.NetworkCredential("user", "pass")

                clsRequest.Method = System.Net.WebRequestMethods.Ftp.MakeDirectory

                Dim response = clsRequest.GetResponse()

            Catch err As Exception

            End Try

        End If

    End Sub

    Public Sub UploadDir(ByVal Game As String, ByVal Path As String, ByVal SourceDir As String, ByVal User As String)

        MakeDir(Game, Path, User)

        SourceDir = SourceDir.Trim()

        For Each File In System.IO.Directory.GetFiles(SourceDir)

            UploadFile(Game, Path, File.Substring(File.LastIndexOf("\") + 1, File.Length - (File.LastIndexOf("\") + 1)), File, User)

        Next

        For Each Directory In System.IO.Directory.GetDirectories(SourceDir)

            UploadDir(Game, Path & "/" & Directory.Substring(Directory.LastIndexOf("\") + 1, Directory.Length - (Directory.LastIndexOf("\") + 1)), Directory, User)

        Next

    End Sub

End Module

解决方案

部分答案是和我一起强迫FTP尝试使路径的所有目录都存在,即使它们已经存在,我也会添加一个简单的检查功能,如下所示:

    公共函数DidDirExist(ByVal游戏为字符串,ByVal路径为字符串,ByVal用户为字符串)

       尝试

            将WebClient更改为新的WebClient()

          将Dim DirStatus设置为String = WebClient.DownloadString("http://server.net/GameSync/" +用户+"/Profiles/" +游戏+"/GameData/" +路径)

  ;           返回True

        异常捕获

            返回False

        结束尝试

     End Function 

,并在空检查之后紧接着将if/then快速插入到makedir()的第一行中

        如果DidDirExist(Game,Path,User)= True,则

           退出子

        

结束,这样有助于避免一些无意义的循环,但是这并不是我真正需要的速度提高...


Ive been working on a program that syncs its data via SQL and FTP to our company server. I wrote the following subs and placed them in a module for easy FTP coding and they work fine but are incredibly slow for only uploading a couple Mb of data and i think the problem lays in there not large files but rather a large amount of files and thus making more FTP connections than necessary. I tried to cut the fat off this as much as possible but it always seems to slow on the deeper recursive directories....

Imports System.IO 

Public Module Sync

    Public Sub UploadFile(ByVal Game As String, ByVal Path As String, ByVal FileName As String, ByVal Source As String, ByVal User As String)

        Path = Path.Trim("/")

        If Not Path = "" Then

            MakeDir(Game, Path, User)

        End If

        Dim sr = New StreamReader(Source, System.Text.Encoding.Default)

        Dim RawFile As String = sr.ReadToEnd

        sr.Close()

        For i = 1 To 5

            Try

                Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://server.net/" + User + "/Profiles/" + Game + "/GameData/" + Path + "/" + FileName), System.Net.FtpWebRequest)

                clsRequest.Credentials = New System.Net.NetworkCredential("user", "pass")

                clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

                Dim bFile = System.Text.Encoding.Default.GetBytes(RawFile)

                Dim clsStream = _

                    clsRequest.GetRequestStream()

                clsStream.Write(bFile, 0, bFile.Length)

                clsStream.Close()

                clsStream.Dispose()

                Exit For

            Catch err As Exception

                If i = 5 Then

                    MsgBox("Unable to upload """ & Source & """ to """ & "ftp://dsdcomputers.net/" + User + "/Profiles/" + Game + "/GameData/" + Path + "/" + FileName & """ after 5 attempts because of the following error:" & vbCrLf & vbCrLf & err.ToString)

                End If

            End Try

        Next

    End Sub

    Public Sub MakeDir(ByVal Game As String, ByVal Path As String, ByVal User As String)

        Path = Path.Trim("/")

        If Path = Nothing Then

            Exit Sub

        End If

        If Path.Contains("/") Then

            Dim PathArray As String() = Path.Split("/")

            Path = ""

            For Each folder In PathArray

                Path = Path & "/" & folder

                Try

                    Path = Path.Trim("/")

                    Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://server.net/" + User + "/Profiles/" + Game + "/GameData/" + Path), System.Net.FtpWebRequest)

                    clsRequest.Credentials = New System.Net.NetworkCredential("user", "pass")

                    clsRequest.Method = System.Net.WebRequestMethods.Ftp.MakeDirectory

                    Dim response = clsRequest.GetResponse()

                Catch err As Exception

                End Try

            Next

        Else

            Try

                Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://server.net/" + User + "/Profiles/" + Game + "/GameData/" + Path), System.Net.FtpWebRequest)

                clsRequest.Credentials = New System.Net.NetworkCredential("user", "pass")

                clsRequest.Method = System.Net.WebRequestMethods.Ftp.MakeDirectory

                Dim response = clsRequest.GetResponse()

            Catch err As Exception

            End Try

        End If

    End Sub

    Public Sub UploadDir(ByVal Game As String, ByVal Path As String, ByVal SourceDir As String, ByVal User As String)

        MakeDir(Game, Path, User)

        SourceDir = SourceDir.Trim()

        For Each File In System.IO.Directory.GetFiles(SourceDir)

            UploadFile(Game, Path, File.Substring(File.LastIndexOf("\") + 1, File.Length - (File.LastIndexOf("\") + 1)), File, User)

        Next

        For Each Directory In System.IO.Directory.GetDirectories(SourceDir)

            UploadDir(Game, Path & "/" & Directory.Substring(Directory.LastIndexOf("\") + 1, Directory.Length - (Directory.LastIndexOf("\") + 1)), Directory, User)

        Next

    End Sub

End Module

解决方案

part of the answer was with me forcing the FTP to try and make all directories of the path even if they already existed, i addad a simple check function as follows

    Public Function DoesDirExist(ByVal Game As String, ByVal Path As String, ByVal User As String)

        Try

            Dim WebClient As New WebClient()

            Dim DirStatus As String = WebClient.DownloadString("http://server.net/GameSync/" + User + "/Profiles/" + Game + "/GameData/" + Path)

            Return True

        Catch err As Exception

            Return False

        End Try

    End Function 

and also put a quick if/then into the first lines of  makedir() right after the null check

        If DoesDirExist(Game, Path, User) = True Then

            Exit Sub

        End If 

so ths helps avoid some pointless loops, but not the increase in speed that i really need here... 


这篇关于我写的一个SLOWWWWW FTP订阅组需要一些帮助.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆