标题:小弟vb菜菜求指点
取消只看楼主
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1817
专家分:3681
注 册:2011-3-24
结帖率:98.24%
已结贴  问题点数:100 回复次数:3 
小弟vb菜菜求指点
源码 : http://www.

还是FTP相关问题~但是感觉已经越来越深入了~已经超出我能理解的范围了~所以特来请教~简单的说我想让程序执行时~
可以依据网路连线状态让程序做出相对应的反应~目前土法炼钢的把断线状态执行程式时~能将错误讯息写到日志档~
也能让程序执行到一半~将网路线拔除后~10秒后程序自动判断断线~能将错误讯息写出~但是问题是瞬断的情况应该如何写?

1.我要如何能够判断有瞬断情况发生?例如断线时启动列的网卡连线状态可以立即反应?我该如何撷取网卡连线状态?

2.我使用的Winsock以UDP方式做的FTP连线~而FTP连线有时并不会因为瞬断导致断线~猜想是未超过TimeOut时间~Socket连线未断~
Port也仍处于Listen的状态中~所以网路再次连线Client/Server双方面仍能保持连线状态~这是我猜的不知道是否如此~

3.最简单的方式~不知道是否能设定让XP中的网卡连线状态能自动产生日志档案~而我程序能在启动后读取某段时间后的连线状态资讯~
再将其写入自身的日志中?

4.我程序是以网路上的FtpConnection此模组为核心~然后再将其改写~先不论这模组本身超多的BUG~此模组运作模式是~
将档切割成每4KB为一组的封包后~如果封包非空值(为何会切出空值这很奇怪?)加上连线状态正常~它才会将封包传出~
但是如果遇上断线情况~超过设定的TimeOut时间时~将导致程序直接当掉~这又是为何?

P.S 此模组本身的BUG目前已修正~包括原本只能使用ASCII模式传档~连线后切换被动模式会导致当机~切出空值封包会导致当机~
执行到一半断线切换目录时也会导致当机~一开始就断线的情况下执行会导致程序无限回圈和无法存取对象等问题会导致当机~
也不支援信件群发功能~没档案的MD5编码计算功能~无法依据档案拓展名自动切换传动档模式~当然也许是它本身是使用手动模式进行开发~
而我把它改写成以另一支程序呼叫BAT档再去呼叫此程序在背景模式下自动执行并且最后自动关闭的型态所导致吧~不过它本身的类就写得很不完善了~
搜索更多相关主题的帖子: 日志 网卡 如何 
2011-10-25 15:05
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1817
专家分:3681
注 册:2011-3-24
得分:0 
回复 2楼 风吹过b
你这方式我写过~然后再让程序去随机产生英美烟草公司档案~再依照档案来源运行指令~

一开始单纯用XP内建的磁区档案复制搬移功能~
后来又改用API的做(因为各种原因)~但是必须得用网芳~
后来才改用(FTP客户改需求)~又说公司都用Winsock的~
所以iNet的什么的DLL的又不能用~FTP的API就是用那元件~
改来改去~快疯了~有时说要用命令行带参数~
有时说要读取的INI设定档~现在最后更新变成存取RES档~
现在全部都有还外加RES档~FTP帐密都使用RES反组译解出~
又要求单一传档需得带进度条~现在又要求程序需得工程自动判断连线状态~
......
为了这程序我总共写了六种版本~客户找碴我也没办法~

不要選我當版主
2011-10-26 18:11
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1817
专家分:3681
注 册:2011-3-24
得分:0 
土法炼钢法~缺点是无法侦测出瞬断~而且因为是用户界面上的控件~反应时间超级慢~经测试我传20个压缩档案共约200MB~
程序启动后看到进度条开始跑~我把网路线的拔掉后~惊人的是,它背景下实际上已经传动完了~服务器上档案都有了~
但是进度条在断线情况下~还是持续的在跑~神奇吧~从日志档来看它花了8秒左右~实际上大约2.3秒就结束了~
程序代码:
Private Sub Ping_Timer_Timer()
Dim Temp() As String, i As Integer
    
    If IsNull(m_FtpConnection) = False Then
        Temp = Split(Ping(m_FtpConnection.FtpServer), vbCrLf)
        For i = 1 To UBound(Temp)
            If Temp(i) <> "" Then
                If InStr(Trim(UCase(Temp(i))), UCase("Request time out")) <> 0 Or InStr(Temp(i), "sendto failed:") <> 0 Then
                    ConnectErrorCount = ConnectErrorCount + 1
                Else
                    ConnectErrorCount = 0
                End If
            End If
        Next i
    Else
        ConnectErrorCount = ConnectErrorCount + 1
    End If
    
    If ConnectErrorCount = 10 Then
        Print #DebugNumber, LabNowProgress.Caption & vbCrLf
        Call ErrorWriteBuff("FTPConnect", 0, ".Connect", Err.Number, Err.Description, "FTP Server not Request !")
    ElseIf ConnectErrorCount > 10 Then
        Call Connect_Error
        Call CloseProgram
    End If
    
End Sub

Private Function Ping(strAddr As String) As String
    GetObject("winmgmts:").Get("NetDiagnostics=@").Ping strAddr, Ping
    Ping = Replace(Ping, "<br>", vbCrLf, 1, , 1)
End Function

不要選我當版主
2011-10-26 20:04
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1817
专家分:3681
注 册:2011-3-24
得分:0 
回复 2楼 风吹过b
你说的大概就是StartProcess()的部分吧~之前写过旦是因为可用的功能太阳春了~就放弃了~贴上一部分代码来证明~

程序代码:
Public Function GetPCName() As String
Dim strComputerName As String
    strComputerName = String(255, Chr$(0))
    GetComputerName strComputerName, 255
    GetPCName = Left(strComputerName, InStr(1, strComputerName, Chr$(0)) - 1)
End Function

Private Sub StartProcess()
Dim i As Integer, FileSize As Long, FileNum As Integer
Dim TempString As String, XMLFile As String, XMLFullFile As String, txtScript As String
Dim CompareStatus As Boolean

On Error GoTo ErrorHandling
    
    CompareStatus = False
    
    txtScript = App.Path & "\FTP.txt"
    XMLFile = "Copy_" & Machine & "_" & Format(Now, "yyyymmdd") & ".xml"
    XMLFullFile = LocalLogPath & XMLFile

    With frmMain
        
        For i = 0 To UBound(FileList)
            Call ReFlashUI(i)
            StartTime = Format(Now, "yyyymmddhhmmss"): FileSize = 0
            .ProgressBar2.ToolTipText = "Copy " & FileList(i) & " To " & FTPData.FTPSite(0).ServerPath & " ~": FileNum = FreeFile
            Open txtScript For Output As #FileNum
                Print #FileNum, GetCode(0, FileList(i))
            Close #FileNum

            If Shell("CMD /k ftp -s:" & txtScript & " >>1.txt", vbHide) <> 0 Then    'Copy
                .ProgressBar2.ToolTipText = "Move " & FileList(i) & " To Backup ~": FileNum = FreeFile
                Open txtScript For Output As #FileNum
                    Print #FileNum, GetCode(1, FileList(i))
                Close #FileNum
                If Shell("CMD /k Move " & LocalFilePath & FileList(i) & " " & BackupFilePath & FileList(i) & ">>1.txt", vbHide) <> 0 Then   'Move
                    EndTime = Format(Now, "yyyymmddhhmmss")
                    Call CompareData(i, CompareStatus)
                    Call WriteXMLFile(i, XMLFullFile, FileSize, CompareStatus)
                Else
                    TempString = "Move " & FileList(i) & " To Backup Error ~"
                    Call ErrorWriteBuff(FileList(i), i, "StartProcess", Err.Number, Err.Description, TempString)
                End If
            Else
                TempString = "Copy " & FileList(i) & " To " & FTPData.FTPSite(0).ServerPath & " Error ~"
                Call ErrorWriteBuff(FileList(i), i, "StartProcess", Err.Number, Err.Description, TempString)
            End If
            MyDoEvents (100)
        Next i
        
        .ProgressBar2.ToolTipText = "Copy " & XMLFile & " To " & FTPData.FTPSite(1).ServerPath & " ~": FileNum = FreeFile
        Open txtScript For Output As #FileNum
            Print #FileNum, GetCode(1, XMLFullFile)
        Close #FileNum
        
        If Shell("CMD /k ftp -s:" & txtScript & ">>1.txt", vbHide) <> 0 Then    'Copy
            Shell "CMD /k Del " & txtScript, vbHide
        Else
            TempString = "FileCopy Error"
            Call ErrorWriteBuff(FileList(i), i, "StartProcess", Err.Number, Err.Description, TempString)
        End If

    End With
    
Exit Sub

ErrorHandling:
    TempString = "FileCopy Error"
    Call ErrorWriteBuff(FileList(i), i, "StartProcess", Err.Number, Err.Description, TempString)
    Resume Next
End Sub

Private Function GetCode(Mode As Integer, tmpFile As String) As String
Dim Temp() As String, TempString As String
Dim i As Integer

    GetCode = "": TempString = ""
    Temp = Split(LocalFilePath, "\")
    
    For i = 0 To UBound(Temp) - 1
        If i = 0 Then
            TempString = TempString & vbCrLf & "lcd " & Temp(i) & "\" & " >>1.txt"
        Else
            TempString = TempString & vbCrLf & "lcd " & Temp(i) & "\" & " >>1.txt"
        End If
    Next i
    
    If Mode = 0 Then
        GetCode = "open " & FTPData.FTPSite(0).ServerPath & " 21" & vbCrLf & _
                    FTPData.FTPSite(0).UserName & vbCrLf & _
                    FTPData.FTPSite(0).UserPass & vbCrLf & _
                    "prompt" & vbCrLf & _
                    "ascii" & vbCrLf & _
                    "mkdir " & LotNumber & _
                    TempString & vbCrLf & _
                    "cd " & LotNumber & vbCrLf & _
                    "put " & tmpFile & vbCrLf & _
                    "bye" & vbCrLf
    Else
        GetCode = "open " & FTPData.FTPSite(0).ServerPath & " 21" & vbCrLf & _
                    FTPData.FTPSite(0).UserName & vbCrLf & _
                    FTPData.FTPSite(0).UserPass & vbCrLf & _
                    "prompt" & vbCrLf & _
                    "binary" & vbCrLf & _
                    "mkdir " & LotNumber & _
                    TempString & vbCrLf & _
                    "cd " & LotNumber & vbCrLf & _
                    "mput " & tmpFile & vbCrLf & _
                    "bye" & vbCrLf
    End If
    
End Function

Private Sub CompareData(i As Integer, CompareStatus As Boolean)
Dim FileSize As Long

    FileSize = FileLen(BackupFilePath & FileList(i))
    If FileSize = ServerFileSize Then
        CompareStatus = True
    Else
        CompareStatus = False
    End If
    
End Sub

Private Sub WriteXMLFile(Findex As Integer, strFileName As String, FileSize As Long, CompareStatus As Boolean)
Dim FileNum As Integer, TempString As String

On Error GoTo ErrorHandling

    FileNum = FreeFile
    If IsFileExist(strFileName) = False Then
        Open strFileName For Output As #FileNum
            Print #FileNum, GetXMLData(Findex, FileSize, CompareStatus)
        Close #FileNum
    Else
        Open strFileName For Append As #FileNum
            Print #FileNum, GetXMLData(Findex, FileSize, CompareStatus)
        Close #FileNum
    End If
    
    AllTime = CStr(CLng(AllTime) + CLng((CLng(EndTime) - CLng(StartTime))))
    
Exit Sub

ErrorHandling:
    TempString = "Write XML Error"
    Call ErrorWriteBuff(LocalFilePath & FileList(Findex), Findex, "WriteXMLFile", Err.Number, Err.Description, TempString)
    Resume Next
End Sub

Private Function GetXMLData(i As Integer, FileSize As Long, CompareStatus As Boolean) As String
Dim Status As String, strVersion As String
    
    strVersion = App.Major & "." & App.Minor & "." & App.Revision
    Status = "": Status = IIf(CompareStatus = True, "PASS", "FAIL")
    GetXMLData = "<DataLog>" & vbCrLf & _
                "    <JobVersion>" & strVersion & "</JovVersion>" & vbCrLf & _
                "    <LotNo>" & LotNumber & "</Lot No>" & vbCrLf & _
                "    <Tester>" & UCase(Machine) & "</Tester>" & vbCrLf & _
                "    <Path>" & LocalFilePath & "<\Path>" & vbCrLf & _
                "    <StartTime>" & StartTime & "</Start Time>" & vbCrLf & _
                "    <EndTime>" & EndTime & "</End Time>" & vbCrLf & _
                "    <FileName>" & FileList(i) & "</File Name>" & vbCrLf & _
                "    <FileSize>" & FileSize & "</File Size>" & vbCrLf & _
                "    <Status>" & Status & "</Status>" & vbCrLf & _
                "</DataLog>" & vbCrLf
                
End Function

Public Sub WriteFiles(FileN As String)
Dim FileNum As Integer, i As Integer
Dim strFileName As String

    If IsFolderExist(LOGSaveLocation) = False Then
        MkDir (LOGSaveLocation)
    End If
    
    strFileName = LOGSaveLocation & FileN
    
    FileNum = FreeFile
    
    If IsFileExist(strFileName) = False Then
        Open strFileName For Output As #FileNum
            If ErrorCount > 0 Then
                Print #FileNum, Format(Now) & "<<---------- Fail ---------->>"
                For i = 0 To UBound(ErrorData)
                    Print #FileNum, ErrorData(i)
                Next i
            Else
                Print #FileNum, Format(Now) & "<<---------- Pass ---------->>"
                Print #FileNum, "Spend " & AllTime & " Seconds"
            End If
        Close #FileNum
    Else
        Open strFileName For Append As #FileNum
            If ErrorCount > 0 Then
                Print #FileNum, Format(Now) & "<<---------- Fail ---------->>"
                For i = 0 To UBound(ErrorData)
                    Print #FileNum, ErrorData(i)
                Next i
            Else
                Print #FileNum, Format(Now) & "<<---------- Pass ---------->>"
                Print #FileNum, "Spend " & AllTime & " Seconds"
            End If
        Close #FileNum
    End If

End Sub

Private Function MyDoEvents(Optional ByVal dwMilliseconds As Long = 1)
   MyDoEvents = DoEvents()
   Sleep dwMilliseconds
End Function

Private Sub ReFlashUI(Findex As Integer)
    With frmMain
        .LabFileCount.Caption = ""
        .LabFileCount.Caption = Findex + 1 & " / " & UBound(FileList) + 1
        .LabNowProgress.Caption = "處理第" & Findex + 1 & "個檔案"
        .ProgressBar2.Value = (Findex / UBound(FileList)) * 100
        .LabAccumulationPercent.Caption = .ProgressBar2.Value & "%"
    End With
End Sub

Public Function IsFileExist(strFileName As String) As Boolean
Dim varFSO As Variant
    Set varFSO = CreateObject("Scripting.FileSystemObject")
    IsFileExist = varFSO.FileExists(strFileName)
    Set varFSO = Nothing
End Function

Public Function IsFolderExist(strFolderName As String) As Boolean
Dim varFSO As Variant
    Set varFSO = CreateObject("Scripting.FileSystemObject")
    IsFolderExist = varFSO.FolderExists(strFolderName)
    Set varFSO = Nothing
End Function

Private Function LoadLocalFile(strFolderName As String) As Integer
Dim varFSO As Variant, varFolder As Folder, varFile As File
Dim i As Integer, TempString As String

On Error GoTo ErrorHandling

    Set varFSO = CreateObject("Scripting.FileSystemObject")
    Set varFolder = varFSO.GetFolder(strFolderName)
    
    For Each varFile In varFolder.Files
        ReDim Preserve FileList(i)
        FileList(i) = Trim(Mid(varFile, InStrRev(varFile, "\") + 1))
        i = i + 1
    Next
    
    frmMain.LabFileCount.Caption = "0 / " & i
    LoadLocalFile = i
    
    Set varFSO = Nothing
    
Exit Function

ErrorHandling:
    TempString = "Get File List Error"
    Call ErrorWriteBuff(LocalFilePath & FileList(i), i, "LoadLocalFile", Err.Number, Err.Description, TempString)
    Resume Next
End Function

Public Function ErrorWriteBuff(FileName As String, lines As Integer, FunctionName As String, code As Integer, Description As String, Remarks As String) As Boolean
    
    If Description = "" Then
        Description = "Null"
    End If
    
    ReDim Preserve ErrorData(ErrorCount)
    ErrorData(ErrorCount) = FileName & ":" & Format(lines, "000") & "  " & FunctionName & "  " & "code :" & code & " Description :" & Description & ":" & Remarks
    ErrorCount = ErrorCount + 1
    
End Function


不要選我當版主
2011-10-27 11:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-353385-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032242 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved