Results 1 to 3 of 3

Thread: help with file transfer through winsock

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2000
    Location
    Canberra - Australia
    Posts
    8

    Question

    hi there thanx for coming to my rescue

    i want to transfer files between server/client, and am having trouble. i have seen examples of source code, but am unable to implement it myself. can you PLEASE tell me what to do or tell me where i can find ---->DETAILED<----information on this topic.

    Thanx
    *Waco_Jaco*

  2. #2
    Guest
    Search http://www.planet-source-code.com for "File Transfer" or "Winsock" or something of that sort.

    I am sure you will find a good example which explains everything.

    Hope you find what you are looking for.
    Good luck!

  3. #3
    New Member
    Join Date
    Jan 1999
    Location
    NYC
    Posts
    14

    Wink File Transfers with Winsock

    Ahh, yes... this is the age-old Winsock question.

    File transfers are not that difficult if you understand the logic behind it. I admit, it's been a little while since I coded a transfer... but I'll try to help you as much as possible.

    Let me start my personal logic (it can differ slightly with different programmers, but the basis remains the same:

    1. Client divides a file up into chunks. Usually about 2048 bytes in size. (but experiment with sizes...don't go about 8000 bytes)
    2. Client tells the server how many chunks are about to be sent and the size of the final chunk. This way, the server knows how many 2048 byte chunks are coming, plus how many bytes are remain in the final chunk.
    3. Client sends a chunk.
    4. Server recieves the chunk and writes it to the disk.
    5. Server send the client a string that tells it that it's done processing and that it wants the next chunk.
    6. Client recieves the "OK" from the server and repeats steps (3-6) until the transfer is complete.

    I think this is pretty much self explanatory... However, in most examples, you won't see the server sending the client an "OK" message before it sends the next chunk... Usually you'll see a pause or timeout of some sort. Although MY method slows down the transfer a tiny bit, it is 100% reilable. You never know when their may be net congestion or a freeze of some sort that lasts longer then the client's pause...then your going to have a big transfer disaster.

    Okay... now for some code. Since I don't have my own code on hand, I'm going to use the code found right here at vb-world.net....it's probably the best you'll find anywhere.... I'll insert my own comments for it... keep in mind, the following code does NOT completely match the logic I listed above since this is only a very basic example.

    Public Sub SendFile(FileName As String, WinS As Winsock)
    Dim FreeF As Integer
    Dim LocData() As Byte
    Dim LenData As Long
    Dim sendloop As Long

    FreeF = FreeFile 'Finds an available File Number to open the file

    Open FileName For Binary As #FreeF 'Open the file you want to transfer as a binary file

    ReDim LocData(1 To 2048) As Byte 'LocData is your buffer. This statement tells the computer that you want the buffer to hold exactly 2048 bytes of information.

    LenData = LOF(FreeF) 'Find how many bytes total are in the file you want to transfer

    For sendloop = 1 To LenData \ 2048 'Loop until all the 2048 byte chunks are sent.
    Get #FreeF, , LocData 'Copy the data from the file to your buffer
    WinS.SendData LocData 'Send the chunk
    Next

    'I want you to note the above loop.... in this vb-world example, the author chose to send the chunks all at once. If you notice at the very end of this code, he inserts a "Sleep" command to let the computer "catch up". Using the method I stated above in my logic, you wouldn't have to worry abou the computer catching up, nor would you send the chunks all at once.

    If LenData Mod 2048 <> 0 Then 'All the 2048 byte chunks have been sent... but what about that final chunk? Unless the length of your file is a multiple of 2048, chances are there's still more of that file to transfer

    ReDim LocData(1 To LenData Mod 2048) As Byte 'This statement redeclares your buffer variable to the length of the final chunk. Note: The "Mod" function gives you the remainder of a division.

    Get #FreeF, , LocData 'Get the final chunk with your redeclared buffer.

    WinS.SendData LocData 'Send the final chunk

    End If

    Close #FreeF ' Close the file

    Sleep 200 ' Let computer catch up

    End Sub


    Okay... and here's the code to receive the the file on the server's end (also taken directly from the vb-world site):


    Dim StrData() As Byte 'Your buffer

    WinS.GetData StrData, vbString 'Get the incoming data.

    'Actually, I believe the correct parameter for the above statement should be vbByte, not vbString. But try vbString first... I don't have the opportunity to test it right now.

    Put #1, , StrData 'Assuming that File #1 is the file you opened to write binary data to, this statement writes the incoming data to the file. (i would change this to another freefile)


    THAT'S IT! I hope I didn't confuse you too much. Keep in mind that the vb-world example and my logic don't match exactly. The example is just to show you a transfer in it's easiest form. The logic is what you should probably aim for in your final project. Hope I was of help. Have fun!

    -Chaz
    [email protected]

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