Results 1 to 14 of 14

Thread: [RESOLVED] Help With File Transfer Again Please..

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    53

    Resolved [RESOLVED] Help With File Transfer Again Please..

    Ok well i got most of it sorted out but i got a new problem an i can't seem to fix it if anyone has time can you optimize my code? the problem i'm having is when i send a file from computer A to computer B , Computer B trys to saves as computer A's directory EG computer A say C:\documents\usernamex\desktop while computer B c:\documents\diffusernamek\desktop so when i send a file to b from a it trys saving as a's directory but it doesnt exist becuase its on a an vice versa?

    Code:
    Private Sub senddataa(sFile As String, sSaveAs As String, tcpCtl As Winsock)
    On Error GoTo ErrHandler
        Dim sSend As String, sBuf As String
        Dim ifreefile As Integer
        Dim lRead As Long, lLen As Long, lThisRead As Long, lLastRead As Long
        
        ifreefile = FreeFile
        
        ' Open file for binary access:
        Open sFile For Binary As #ifreefile
        lLen = LOF(ifreefile)
        
        ' Loop through the file, loading it up in chunks of 64k:
        Do While lRead < lLen
            lThisRead = 65536
            If lThisRead + lRead > lLen Then
                lThisRead = lLen - lRead
            End If
            If Not lThisRead = lLastRead Then
                sBuf = Space$(lThisRead)
            End If
            Get #ifreefile, , sBuf
            lRead = lRead + lThisRead
            sSend = sSend & sBuf
        Loop
        lTotal = lLen
        Close ifreefile
        bSendingFile = True
        '// Send the file notification
        tcpCtl.senddata "FILE" & sSaveAs
        DoEvents
        '// Send the file
        tcpCtl.senddata sSend
        DoEvents
        '// Finished
        tcpCtl.senddata "FILEEND"
        bSendingFile = False
        Exit Sub
    ErrHandler:
        MsgBox "Err " & Err & " : " & Error
    End Sub
    Code:
    Private Sub Winsock2_DataArrival(ByVal bytesTotal As Long)
        Dim strData As String
        Dim ifreefile
    
        Winsock2.getdata strData
        If Right$(strData, 7) = "FILEEND" Then
            bFileArriving = False
            sArriving = sArriving & Left$(strData, Len(strData) - 7)
            ifreefile = FreeFile
                Open sFile For Binary As #ifreefile
                Put #ifreefile, 1, sArriving
                Close #ifreefile
            lblProgress = "File Transfer Complete"
        ElseIf Left$(strData, 4) = "FILE" Then
            bFileArriving = True
            sFile = Right$(strData, Len(strData) - 4)
        ElseIf bFileArriving Then
            lblProgress = "Receiving " & bytesTotal & " Bytes For " & sFile & " From " & Winsock1.RemoteHostIP
            Label5.Caption = "Connected"
            sArriving = sArriving & strData
        End If
    End Sub
    
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
        Dim strData As String
        Dim ifreefile
    
        Winsock1.getdata strData
        If Right$(strData, 7) = "FILEEND" Then
            bFileArriving = False
            sArriving = sArriving & Left$(strData, Len(strData) - 7)
            ifreefile = FreeFile
                Open sFile For Binary As #ifreefile
                Put #ifreefile, 1, sArriving
                Close #ifreefile
            lblProgress = "File Transfer Complete"
        ElseIf Left$(strData, 4) = "FILE" Then
            bFileArriving = True
            sFile = Right$(strData, Len(strData) - 4)
        ElseIf bFileArriving Then
            lblProgress = "Receiving " & bytesTotal & " Bytes For " & sFile & " From " & Winsock1.RemoteHostIP
            sArriving = sArriving & strData
        End If
    
    End Sub
    Code:
    Private Sub Command4_Click()
        If Dir$(txtFile) = "" Then
            MsgBox "File Does Not Exist"
        Else
            senddataa txtFile, GetFileName(txtFile), Winsock1
        End If
    End Sub
    
    Private Sub Command5_Click()
        If Dir$(txtFile) = "" Then
            MsgBox "File Does Not Exist"
        Else
            senddataa txtFile, GetFileName(txtFile), Winsock2
        End If
    End Sub
    Code:
    Public Function GetFileName(flname As String) As String
    Dim posn As Integer, i As Integer
    Dim fName As String
        
    posn = 0
    For i = 1 To Len(flname)
    If (Mid(flname, i, 1) = "\") Then posn = i
    Next i
    fName = Right(flname, Len(flname) - posn)
    GetFileName = fName
    End Function
    I make command 4 visible when its the listener or host an command5 hidden vice versa if they connected other way can anyone help
    Last edited by Evilribbet; Jan 8th, 2009 at 02:00 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width