Results 1 to 11 of 11

Thread: Winsock file transfer

  1. #1
    Guest

    Question

    I'm using the following code for a file transfer:
    Code:
    Private Sub WinS_DataArrival(ByVal bytesTotal As Long)
    On Error Resume Next
    If WinS.Tag = "OK" Then
        Open App.Path & "\File.exe" For Binary As #1
    End If
    MsgBox "Recieved"
    WinS.Tag = ""
    Dim StrData() As Byte
    
    WinS.GetData StrData, vbString
    Debug.Print StrData
    
    Put #1, , StrData
    End Sub
    WinS.Tag = "OK" when the transfer button is pushed.

    Here's the question. How can I close #1 when the whole file is completed. Help me please!

  2. #2
    Guest
    i am new to this but would this work at the end


    Put #1, , StrData
    Close #1

  3. #3
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    conquerdude, your server (Transmit the file) should must inform the client (Receive the file) that the entire file is completed sent & the client will now save to close the file.

    To do this, you need to involve some own protocol between the server and client communication and it may need a big modification in your existing coding. But that the way to do a file transfer with WinSock. Else you can look for the FileCopy function that come with Visual Basic.

    Hope this little word can help you.

    Cheers!



  4. #4
    Guest
    well as u can tell from the above reply i was way off




    hey i said i was new



    but i tried

  5. #5
    Guest

    So would i do this?

    If I open #1 in one sub could I close it in another?
    Example:
    Code:
    'This is just an example, not what I'm using
    Private Sub Command1_Click()
    Open "C:\Test.tst" For Input As #1
    End Sub
    
    Private Sub Command2_Click()
    Close #1
    End Sub
    Would this work?

  6. #6
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Yes you can, but you need to declare like this way.

    Code:
    Option Explicit
    Private TxFile As Long
    
    Private Sub Command1_Click()
    TxFile = FreeFile
    Open "C:\Test.tst" For Binary Access Write As #TxFile
    End Sub
    
    Private Sub Command2_Click()
    Close #TxFile
    End Sub

    [Edited by Chris on 01-04-2001 at 11:31 PM]

  7. #7
    Guest
    Do I really need the option explict?

  8. #8
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    No, but with the Option Explicit the VB will check all the variable it is properly declared. Else it will propmt you about those undeclare variable and stop proceed to compile or run your coding.

    I personal always include the Option Explicit in all my code.

    Cheers!

  9. #9
    Guest
    How would I be able to open a file in one form and close it in another?

  10. #10
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up

    All you need is add a Basic Module file as below will do.

    Code:
    Option Explicit
    Private TxFile As Long
    
    Public Sub OpenFile(ByVal lpSource As String)
       TxFile = FreeFile
       Open lpSource For Binary As #TxFile
    End Sub
    
    Public Sub CloseFile()
       Close #TxFile
    End Sub
    
    Public Sub WriteFile(ByVal lpData As String)
       Put #TxFile, , lpData
    End Sub
    
    Public Function ReadFile() As String
       Dim str As String
       str = String(LOF(TxFile), Chr(0))
       Get #TxFile, 1, str
       ReadFile = str
    End Function
    Then you can call the following function at anywhere anytime in your form:-
    [*]OpenFile[*]CloseFile[*]ReadFile[*]WriteFile

    Cheers!

  11. #11
    Guest

    Thumbs up

    Thanks a lot!

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