Results 1 to 11 of 11

Thread: How to receive a file and save it

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    87

    How to receive a file and save it

    I've been trying to send a file for more then 3 days but couldnt make it the file is very small so the size should not be a problem

    This code is where I try to send the file I think it works fine
    but my problem I guess is with receiving the file and saving it somewhere

    Code:
    Dim Data As String
    
    Open path For Binary As #1 'path is some dir to a file
    
    Do While Not EOF(1)
    Data = Input(chunk, #1) ' sending the file in parts chunk = 8000
    ws.SendData "File#"  & Data
    
    DoEvents
    Loop
    
    ws.SendData "#" & "End"
    Close #1
    This code is for receiving the file

    Code:
    Private Sub ws1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    
    
    Dim sData As String
    Dim Data() As String
    
    ws(Index).GetData sData
    Data() = Split(sData, "#")
    
    
    Select Case Data(0)
        Case "File"
        Open "thepath" For Binary As #1
        Put #1, , Data(1)
         
    
        Case "End"
        Close #1
    End Select
    Shouldnt this work???

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to receive a file and save it

    My Suggestion: Instead of opening the file beforehand... write the incoming data to a variable and then write it to a file in the end...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    87

    Re: How to receive a file and save it

    Hmm... not working.... Now im not get any data... my problem is that I only get a part of the data and then error saying file already open... im only getting 8 kb data no more

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to receive a file and save it

    When you write the data, move the file cursor to the end of the file....
    Put #1, LOF(1) + 1 , Data(1)
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    87

    Re: How to receive a file and save it

    Not working too ......hmm always getting file "alreday opened"

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to receive a file and save it

    Are you reading and writing using the same project? If so, one routine has #1 open while the other is trying to write to the same file number.

    I modified your code a bit with some comments
    Code:
    Private Sub ws1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim sData As String
    Dim Data() As String
    Dim fNr As Integer
    
    ws(Index).GetData sData
    Data() = Split(sData, "#")
    
    If UBound(Data)<1 Then  ' simple error checking
          MsgBox "Oops, incoming data is garbled", vbInformation + vbOkOnly
          Exit Sub ' error you didn't get what you expected
    End If
    
    Select Case Data(0)
        Case "File"
             fNr = FreeFile() ' always use FreeFile, don't hardcode file numbers
             Open "thepath" For Binary As #fNr
             Put #fNr, LOF(fNr)+1, Data(1)
             Close #fNr
    
        Case "End"
              MsgBox "Got last bit of the file", vbInformation + vbOkOnly ' debugging use only
        End Select
    End Sub
    Code:
    Dim Data As String
    Dim fNr As Integer
    
    fNr = FreeFile() ' always use FreeFile, don't hardcode file numbers
    Open path For Binary As #fNr 'path is some dir to a file
    
    Do While Not EOF(fNr)
    Data = Input(chunk, #fNr) ' sending the file in parts chunk = 8000
    ws.SendData "File#"  & Data
    
    DoEvents
    Loop
    Close #fNr 
    ws.SendData "End#"
    Edited: If you want to keep the destination file open until the entire file stream is received, you can do that, but recommend caching the file number from FreeFile either as a Static variable or in the declarations section. The problem with leaving it open is that if you never get an #End statement in your winsock, the file will never be closed. By opening and closing on each data burst, you get past that problem.

    Hmmm. You were never getting an End# in your routine. You are splitting on #, but you are sending #End and should've been sending End#, per your Select Case & Split statements. That was probably your initial problem. But since you were hardcoding file numbers and not closing the file because of that typo, it makes sense you were getting "file already open" errors. That should be fixed immediately; patched in the code above.
    Last edited by LaVolpe; Aug 24th, 2009 at 07:20 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    87

    Re: How to receive a file and save it

    Thank u for answering... yeah that's right my file was never closed before sending the end statement also mistyping in the end statement which should be "End#"...
    Now there is no error saying file already open but however Im stil getting an error....
    The first error is that the incoming data is garbled and got only 16 kb data... then I removed this error control code so I got a message saying that I got all the data but the true is that I got about only 50 &#37; of my data I tried to send a jpg image with it to test it the first time I got about 70% of the image but couldnt open it an the second time I got about more than 50 % but could open the image but it showed a part of the image and not the whole image.... hmmm.......... dont know what could be wrong with it ... the code just looks fine to me.......

    and about ur question, no the code is not in the same project so string, var names should not be a problem....

    Edit: Im using CSocketMaster could that be the problem?
    Last edited by Milad87; Aug 24th, 2009 at 09:36 PM.

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to receive a file and save it

    I do see a flaw in your code, but it may not be related to your problem.

    You are splitting on the # character. If the data you send has that ascii value in the data, then you will split that too. UBound(Data) will be > 1, but you are only writing Data(1) to the file. Recommend splitting on File# vs just #. And that is assuming you never pass a file with the characters File# in it.

    If tweaking how you split the data doesn't fix the problem, search this forum for "Winsock incomplete data" or similar terms and see if other posts can help. There are some comm gurus on this site, I am not one of them, unforunately.

    Another option may be to send less data, maybe 1 or 2kb vs nearly 8kb at a time. What might be happening is that the entire 8kb isn't sent in one packet and when the rest of the packet comes in, your Split is looking for a # sign that isn't there, so you are not recognizing the partial packet and not writing it to file.
    Last edited by LaVolpe; Aug 24th, 2009 at 09:45 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    87

    Re: How to receive a file and save it

    Well, thank you for helping .... I will search the forum and google it if I cant find anything.. thank you again

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to receive a file and save it

    Look at these two posts by CVMichael, one of the site's better comm members.
    http://www.vbforums.com/showthread.php?t=377648
    http://www.vbforums.com/showthread.php?t=331990
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    87

    Re: How to receive a file and save it

    Thanks I have seen these posts.. and the idea is the same but the problem is the code

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