Results 1 to 39 of 39

Thread: File transfers with UDP: Bad idea?

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43

    Exclamation

    is it a bad idea to use udp? ..ive already noticed it doesnt exactly work with bianary files, and skips data...

    but...is it a bad idea to use udp for file transfers?, i read on msdn...they dont recommend it..but it works with ascii files.
    .:: jier :: 545 Studios ::.

  2. #2
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    why not use TCP/IP protocol?

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43

    Well...

    Tcp is a pain in the ass..and you must take more steps when making a clent/server in one..

    with tcp, you have to listen, accept, close, connect...
    with udp.. you send

    im trying to find the fastest/easiest way to do this.
    .:: jier :: 545 Studios ::.

  4. #4
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    em... I'll prefer tcp as I can have more than 2 point communication as compare to UDP. which juz offer peer 2 peer communication. Also I can send what ever I wish to other remote terminal

  5. #5
    Guest
    Well the nature of UDP prevents effecient file transfer...

    UDP packets come out of order, or some don't arrive at all. TCP packets are more or less guaranteed to come in-order because a lot of program logic in Winsock.dll makes sure thay arrive and are re-sent if not...

    TCP is a lot easier in the long run, you can use UDP, but you'll hace to add a lot of logic to compensate for lost and out-of-order packets.

    Gerco Dries.

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43

    hehe

    i guess you cant always take the easy way out..i recoded it to tcp, and binary files are still missing chunks of data.. take a look at my download code..

    this is in data arival

    If Left(fData, 3) = "FD:" Then
    Put #2, , Mid(fData, 3, Len(fData))
    End If

    when i send the data, i add "FD:" to the left ( file data ) so we know for sure its data for the file.
    i really dont see anything wrong here...do you?
    .:: jier :: 545 Studios ::.

  7. #7
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    jier, please ensure the data have been completely send out then only you proceed to the next block of data. Juz likr below:

    Code:
    Option Explicit
    Private SEND_COMPLETE As Boolean
    
    Private Sub Command1_Click()
        SEND_COMPLETE = False
        If Winsock1.State <> sckClosed Then
            Winsock1.SendData "data here....."
            Do While Not SEND_COMPLETE
                '//Juz wait till teh data is competely send
                DoEvents
            Loop
        End If
    End Sub
    
    Private Sub Winsock1_SendComplete()
        SEND_COMPLETE = True
    End Sub

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43

    i never thought of that..

    stupid mistakes are always the problem. lol
    thanks..i'll try it out..let you know the results.
    .:: jier :: 545 Studios ::.

  9. #9

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43

    hmmm..

    now im getting ano overflow..

    Open dlg.FileName For Binary Access Read As #1
    fsize = LOF(1) ' <-- overflow?
    Close #1
    .:: jier :: 545 Studios ::.

  10. #10
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Make sure you've declare the fsize to long datatype.

  11. #11

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43

    oops

    fname wasnt long..i'll fix that ( thanks for another tip! )


    ok..i tried the do while method..

    Do While Not EOF(1)
    Temp = Space$(BlockSize)
    Get 1, , Temp
    SendComplete = False
    wFiles.SendData "FD:" & Temp
    Do While Not SendComplete
    DoEvents ' <- gets caught here
    Loop
    Loop

    But it gets caught in the loop...and i didnt forget to add the sendcomplete=true in the sendcomplete proc..im not sure whats wrong now
    .:: jier :: 545 Studios ::.

  12. #12
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    jier, here is how I send a file to remote PC through Winsock control.

    Code:
    Private SEND_COMPLETE As Boolean
    
    Private Sub Send2Client()
        Dim tmp As String
        Dim bSz As Long
        Dim fSz As Long
        
        'Define buffer size
        bSz = 1024
        Open "C:\MyData.txt" For Binary As #1
            '//Get File Size
            fSz = LOF(1)
            Do Until EOF(1)
                 '//Don't read too much at the end.
                 If fSz - Loc(1) <= bSz Then
                     bSz = fSz - Loc(1) + 1
                 End If
                 
                 '//Read a block of data.
                 tmp = Space$(bSz)
                 Get #1, , tmp
            
                 '//Transmit the block.
                 Winsock1.SendData tmp
                 
                 '//Wait for all the data to be sent.
                 Do While Not SEND_COMPLETE
                     DoEvents
                 Loop
             Loop
        Close #1
    End Sub
    
    Private Sub Winsock1_SendComplete()
        SEND_COMPLETE = True
    End Sub

  13. #13

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43

    thanks

    lemme try to fix up my code by looking at yours..i hope it works!
    .:: jier :: 545 Studios ::.

  14. #14

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43

    unf

    this sucks..its not sending data, also..i have a question..Is it better to write the data to the file as it comes? or store it until it has fully downloaded..then write it to a file?
    .:: jier :: 545 Studios ::.

  15. #15
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Jier, in order to transfer a file, you need to send an Instruction to the remote PC. So that it will open a new file to save the received data. Again, after you completed sending the entire file, you need to instruct the remote PC to close the file handle.

    So the entiree file transfer protocol will look like this:

    Code:
        Server                                             Client
        *FileName (Test.txt)
        *FilePath (C:\)
                                 Instruction
                                   ------> 
                                                        Client Create a new file
                                                        (E.g.: C:\Test.txt)
                                                        with the supplied info
                                                        *FileName
                                                        *FilePath
                               Ackowledgement 
                                   <------
    
        Start read the source
        File Block By Block
        and send to Client
        until end of file
    
        (The following routine
        will be carried out
        until end of the source
        file)
    
                        File Transfer Routine commence
                                   ------->
    
                                                        Client acknowledge data
                                                        correctly save to file.
                               Ackowledgement 
                                   <------
        
        Send Closes file 
        command to Client
                                 Instruction
                                   ------> 
                                                        Client proceed close the
                                                        newly create file
                                                        (E.g.: C:\Test.txt)
                                                        And acknowledge server
                                                        about successful close
                                                        the file.
    
                               Ackowledgement 
                                   <------

  16. #16

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43

    yeah

    i hope im not making it seem like i have absolutley no idea what im doing here.

    I realize howto transfer files, but am having problems sending data, and reciving it **correctly** thats all
    .:: jier :: 545 Studios ::.

  17. #17
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    jier, you can save the receive data into the file at file or block by block.

    or perhaps you can post your code?

  18. #18

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43
    after sending my header command, i check for the FD command, left of data, fd being file data, it filters out the command, and write to the previously opened file.


    If Left(fData, 3) = "FD:" Then

    Debug.Print fData & " <- Sent data"

    Put #2, , Mid(fData, 3, Len(fData))
    End If
    .:: jier :: 545 Studios ::.

  19. #19

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43
    ofcourse i have code that closes the file when done downloading..but that wasnt posted because it doesnt have any effect on receiving the data.
    .:: jier :: 545 Studios ::.

  20. #20
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Code:
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
        Dim Buffer As String
        
        '//Downlaod data from Winsock
        Winsock1.GetData Buffer, vbString
        
        '//Check HEADER
        If Len(Buffer) <> 0 Then
            Select Case Mid(Buffer, 1, 3)
            Case "FD:" 'Server Path
                Put #2, , Mid(Buffer, 4)
                
                '//Acknowledgement back to server abt the correctly save the data.
                Winsock1.SendData "ACK#"
            Case Else
                Put #2, , Buffer
                
                '//Acknowledgement back to server abt the correctly save the data.
                Winsock1.SendData "ACK#"
            End Select
        End If
    End Sub
    Last edited by Chris; Feb 10th, 2001 at 03:21 AM.

  21. #21

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43
    using getdata...
    .:: jier :: 545 Studios ::.

  22. #22
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    it is like what i did?

  23. #23

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43

    yeah..

    i didnt know there was any other way to do it.
    .:: jier :: 545 Studios ::.

  24. #24
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    did you try out my method?

  25. #25

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43
    sure did..


    Dim tmp As String
    Dim bSz As Long
    Dim fSz As Long

    'Define buffer size
    bSz = 1024
    If FileToSend = "" Then Exit Function
    Close #1
    Open FileToSend For Binary Access Read As #1

    '//Get File Size
    fSz = LOF(1)
    Do Until EOF(1)
    '//Don't read too much at the end.
    If fSz - Loc(1) <= bSz Then
    bSz = fSz - Loc(1) + 1
    End If

    '//Read a block of data.
    tmp = Space$(bSz) ' <- returns null
    Get #1, , tmp
    '//Transmit the block.
    wFiles.SendData "FD:" & tmp
    '//Wait for all the data to be sent.
    Do While Not SendComplete
    DoEvents ' <- gets caught here still
    Loop
    Loop
    Close #1
    .:: jier :: 545 Studios ::.

  26. #26
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    what do you mean "<- returns null "
    Are you encounter error in that line of code?

  27. #27

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43
    no, tmp returns nothing.. tmp = ""
    .:: jier :: 545 Studios ::.

  28. #28
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Jier, The code that I show you is working correctly as I tested juz now.

    Perhaps you need to check the file that you require to send is physically in the given location before you proceed to read it contents.

    Code:
        If Len(Dir(FileToSend)) = 0 Then Exit Function
        Open "C:\winzip.log" For Binary Access Read As #1
    Also, you also can check this out by reading the fSz value:

    Code:
        fSz = LOF(1) 
        MsgBox "Read File size = " & fSz

  29. #29

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43
    heres the full send code, It prints the data to the debug window, and...i'll show you that its empty..


    code:
    If wFiles.State <> sckClosed Then
    Dim tmp As String
    Dim bSz As Long
    Dim fSz As Long

    'Define buffer size
    bSz = 1024
    If FileToSend = "" Then Exit Function
    If Len(Dir(FileToSend)) = 0 Then Exit Function
    Close #1

    Open FileToSend For Binary Access Read As #1

    '//Get File Size
    fSz = LOF(1)
    Do Until EOF(1)
    '//Don't read too much at the end.
    If fSz - Loc(1) <= bSz Then
    bSz = fSz - Loc(1) + 1
    End If

    '//Read a block of data.
    tmp = Space$(bSz)
    Get #1, , tmp
    '//Transmit the block.
    wFiles.SendData "FD:" & tmp
    Debug.Print tmp & " <- data to send"
    Do While Not SendComplete
    DoEvents
    Loop
    Loop
    Close #1
    Else
    'not connected.
    End If


    what it says in the debug window:
    FD: <- Sent data



    basicly..its not getting data for some reason...how do we find out why?
    .:: jier :: 545 Studios ::.

  30. #30
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Jier, why you need to close the filehanfle #1?

    Code:
        If FileToSend = "" Then Exit Function 
        If Len(Dir(FileToSend)) = 0 Then Exit Function
    Close #1
    What is the data in the file that you try to read?

  31. #31
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158
    UDP is best used for steaming purposes, so unless your making somthing to do with streaming use TCP/FTP.

    Remember it's a pain in the ass for the programmer but fun for the user, bucks to the programmer and an invoice to the user

  32. #32

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43
    i close 1, just incase it was left open...the data is whatever the user sends, zip, bmp, txt, whatever
    .:: jier :: 545 Studios ::.

  33. #33
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    jier, try to change the Space$ to String as show below:

    Code:
          tmp = Space$(bSz)
          tmp = String(bSz, Chr(0))

  34. #34
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Also, please ensure you have passing the fullpath of the file that you try to read, like

    FileToSend = "C:\...\Mydata.txt

  35. #35

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43
    tried it..no luck.. hmmmmm

    this is turning into a major task
    .:: jier :: 545 Studios ::.

  36. #36
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Em... then change the #1 to #hFile and see will it work?

    Code:
    Dim tmp As String
    Dim bSz As Long
    Dim fSz As Long
    Dim FileToSend As String
    Dim hFile As Long
        
        FileToSend = "C:\Winzip.log"
        If Len(Dir(FileToSend)) = 0 Then Exit Function
    
        'Define buffer size
        bSz = 1024
        
        hFile = FreeFile
        Open FileToSend For Binary Access Read As #hFile
        
            '//Get File Size
            fSz = LOF(hFile)
        
            Do Until EOF(hFile)
                '//Don't read too much at the end.
                If fSz - Loc(hFile) <= bSz Then
                    bSz = fSz - Loc(hFile) + 1
                End If
    
                '//Read a block of data.
                tmp = Space$(bSz)
                Get #hFile, , tmp
                '//Transmit the block.
                wFiles.SendData "FD:" & tmp
                '//Wait for all the data to be sent.
            
                Do While Not SendComplete
                    DoEvents ' <- gets caught here still
                Loop
            Loop
        Close #hFile

  37. #37

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    usa
    Posts
    43
    Well, That worked!...so, can you explain why it was doing that?
    .:: jier :: 545 Studios ::.

  38. #38
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Both #1 and #hFile is work on my PC. Perhasp you've use #1 before this routine?

    Also, the Open function does support the filenumber range from 1 to 512. But it must be close before it can be use again.

  39. #39
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Being the obnoxious lazy git that I am, I've not bothered to read the whole thread, so all I'll say is that you should not ever ever use UDP for things that require that all data reach the destination.

    Use UDP for things like video-conferencing etc.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

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