Results 1 to 4 of 4

Thread: Winsock+Open A File==>{EXPERT}

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 1999
    Location
    Kitchener Ontario Canada
    Posts
    20

    Angry

    Hello! I am in a little bit of a "jam" here!! I am creating a Winsock Client/Host application..

    I have everything the way i want so far, ., Except for one little problem! When i want the host portion of my program to send a file.. The program first has to open this specific file into a Binary Variable and then it begins sending it into chunks!

    ***************************************************
    Public Sub SendFile(FileName As String, Transfer As Winsock)
    Dim FreeF, Stoper As Integer
    Dim LocData(1 To 2048) As Byte

    FreeF = FreeFile

    Open FileName For Binary As #FreeF

    Do While Not EOF(FreeF)

    Get #FreeF, , LocData 'Get data from the file nCnt is from where to start the get

    Transfer.SendData LocData 'Send the chunk

    Loop

    Close #FreeF

    Sleep 200

    End Sub
    ************************************************
    ////////////////////////////////////////////////

    But the Problem with this is that if i want to send a larger file (lets say 50MB++) .,,. the program really uses up the (HOST) HD space becasue it stores the whole contents of the file in to 1 variable in my vb application , and then it gradually begins sending the chunks of data.

    I guess what i am primarily asking, is , is there a way to open a specific part of a file and load it into a variable.,.,

    Lets say, i want the first 200 bytes of a file loaded into a variable withought having loaded the whole file into my application!

    and then proceeding from the 200 byte section to the 400 byte section.,.,giving me again another 200 byte chunk of data.(200 byte was used just for example , doesn't have to be 200)

    To clarify this a little bit more,, I want to open up a specific part of a file in to a variable withought having first gone through the whole contents of the file!

    *******************************************
    I know this is long, But i would really appreciate it if anyone helped me here!
    *******************************************

  2. #2
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    What's wrong in your code?

    After a careful read of your code, it seems fine to me. You are breaking the file down into 2048 byte chunks and sending each chunk.

    YOu said that your problem is that you read the entire file into memory, but where does this happen?

    Cheers
    Paul Lewis

  3. #3
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    OK, another careful read...

    This time I read it and got the idea you might be wanting to restart the reading from a particular point (say 25MB of that 50MB whopper?). So perhaps you are writing code to automatically resume an aborted upload?

    In that case, pay special attention to the line in your code:

    Code:
    Get #FreeF, , LocData 'Get data from the file nCnt is from where to start the get
    Looking at the comment here, I presume you have cut and pasted this from someplace else? No matter if you did, just keep in mind that the Get # command allows as the second parameter an offset value. This means you can specify where to start the reading from (i.e. the exact byte).

    Alternatively you use the seek procedure to point the open file to the starting point.

    Hope it helps
    Paul Lewis

  4. #4
    Guest
    Here is some code that will help you. It will open the file and read the amount specified by chunksize and automatically advance through the file. When it gets to the last chunk, which is likely to be smaller than the chunksize, the chunksize will automatically re-adjust its size to accomdate for the last chunk.


    Code:
    'sending code
        'open in binary
        Open FullFilename For Binary Access Read As #1  
            Do Until EOF(1)
                If StopNow = True Then   'sudden stop
                    'rest the variable so it won't stop itself
                    StopNow = False  'next time round
                    Close #1 
                    Exit Do  'exit the loop
                End If
                're-adjust chunksize if it is too large
                If FSize - Loc(1) <= Chunksize Then
                    Chunksize = FSize - Loc(1)
                    'indicates this is the last packet
                    Last = True  
                End If
                SData = Space$(Chunksize)   'make empty string
                Get #1, , SData 'get data into var
                wskfile.SendData SData  'send it
                DoEvents
                If Last = True Then 
                    'exit loop because of its last packet
                    Last = False
                    Exit Do
                End If
            Loop
        Close #1

    and the receiveing code

    Code:
    Private Sub wskFile_DataArrival(ByVal bytesTotal As Long)
        Dim Incoming As String
        Dim count As Integer
        'bytesTotal is set to the size of the file
        bytesTotal = FSize   
    
        If StopNow = True Then
            Exit Sub
        End If
        
        wskFile.GetData Incoming
        
        Put #2, , Incoming     'write data into file
        'if the size of the received file is equal to the       
        'filesize the stop
        If LOF(2) = FSize Then           
            Close #2  
            Wait (1)
            Call ResetConnection
        End If
    End Sub
    Sunny

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