Results 1 to 4 of 4

Thread: WinSock - File Transfer

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2001
    Posts
    2

    Post

    Hey all,

    I've been trying to transfer some files between two networked computers using the code below. It works fine for simple text but JPEGs and I'm guessing other things don't work.

    Could anybody please explain to me why this doesn't work for JPEGs and how I transfer JPEGs, EXEs, ZIPs using WinSock?


    Public Sub SendData(sFile As String, sSaveAs As String, tcpCtl As Winsock)
    On Error GoTo ErrHandler
    Dim sSend As String, sBuf As String
    Dim ifreefile As Integer
    Dim lRead As Long, lLen As Long, lThisRead As Long, lLastRead As Long

    ifreefile = FreeFile

    ' Open file for binary access:
    Open sFile For Binary Access Read As #ifreefile
    lLen = LOF(ifreefile)

    ' Loop through the file, loading it up in chunks of 64k:
    Do While lRead < lLen
    lThisRead = 65536
    If lThisRead + lRead > lLen Then
    lThisRead = lLen - lRead
    End If
    If Not lThisRead = lLastRead Then
    sBuf = Space$(lThisRead)
    End If
    Get #ifreefile, , sBuf
    lRead = lRead + lThisRead
    sSend = sSend & sBuf
    Loop
    lTotal = lLen
    Close ifreefile
    bSendingFile = True
    '// Send the file notification
    tcpCtl.SendData "FILE" & sSaveAs
    DoEvents: DoEvents: DoEvents: DoEvents

    '// Send the file
    DoEvents: DoEvents: DoEvents: DoEvents
    ConnectToClient
    tcpCtl.SendData sSend
    DoEvents: DoEvents: DoEvents: DoEvents

    '// Finished
    DoEvents: DoEvents: DoEvents: DoEvents
    ConnectToClient
    tcpCtl.SendData "FILEEND"
    DoEvents: DoEvents: DoEvents: DoEvents
    bSendingFile = False
    Exit Sub
    ErrHandler:
    MsgBox "Err " & Err & " : " & Error
    End Subl


    Thanks for your time,

  2. #2
    Member
    Join Date
    Apr 2001
    Location
    Lake Charles, LA, USA
    Posts
    43

    Talking File Transfers w/ Winsock

    I know what you mean. I had the same problem with my program. The reason it won't transfer the files is that the server is sending the information to the client's winsock control so fast that it can't keep up. The winsock control on the server finishes sending the file long before it finishes arriving at the client. You have to add some timer code to your Do...Loop to reduce the amount of time between when winsock sends data. I've attached a file (both.zip) that I found somewhere that helped me out a lot. It includes both a server application and a client application. I wouldn't use their code verbatim as I don't totally agree with their programming style, however I borrowed a lot of concepts from them such as setting up certain commands to tell your programs to do different things such as 'sckServer.Send "OpenFile," & filename$' (You'll understand better if you study the code in this zip file). In this demo, they set the time to pause between sending data at 200. In my program, that wasn't enough. I'd recommend using about 500. I've had better luck with that number. Be sure to set a pause between each command you send back and forth as well as a pause between your Do...Loop. I think this should help you, but if you have any questions you can catch me on AIM. My s/n is Fulgore8292. You can also try e-mailing me at [email protected], however I don't check my e-mail very often. I'll also try to check this thread every so often to see if this helped or not. Good luck with your program!
    Attached Files Attached Files

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2001
    Posts
    2

    Post

    Excellent. Thank-you!

    I also found a website last night with a download explaining things in a similar fashion.

    If anybody wants that ZIP, email me and I'll send it to you.

    By using these ZIPs, a wall and my head moving at an incredible velocity, I managed to get some kind of transfer procedure.

    It does what I want it to do. But for how long?!

    The problem with these file transfers and some other WinSock things is that they have to be so specialised for whatever you are doing and no-one seems to have come up with a definitive, generalised set of code.

    Thanks for your help,

  4. #4
    Member
    Join Date
    Apr 2001
    Location
    Lake Charles, LA, USA
    Posts
    43

    Lightbulb File Transfer w/ Winsock (Update)

    I'm glad that that zip file was helpful, but I made a mistake.

    I recently realized that setting a Pause command in your Do...Loop will not only limit the maximum upload speed of your program, but it also doesn't always work. Instead of setting a Pause 500 command in your Do...Loop, it would be much better if you have the client tell the server when it's recieved the data. In other words, do something like this:

    Do While lRead < lLen
    Do
    If Waiting = False then Exit Do
    DoEvents
    Loop
    lThisRead = 65536
    If lThisRead + lRead > lLen Then
    lThisRead = lLen - lRead
    End If
    If Not lThisRead = lLastRead Then
    sBuf = Space$(lThisRead)
    End If
    Get #ifreefile, , sBuf
    lRead = lRead + lThisRead
    tcpCtl.SendData sBuf
    Waiting = True
    DoEvents
    Loop

    You would have to add this to the general declarations section of your code...

    Dim Waiting As Boolean

    You would then add something like this to the DataArrival event of your winsock control on the server side...

    Select Command$
    Case "SendAgain"
    Waiting = False
    End Select

    Next, add some code to your client program to send a command to the server telling it that it's ok to send more data like this...

    sckClient.SendData "SendAgain,"

    This should work much better than the Pause function.

    I hope this helps. If you have any questions about this code, be sure to post them.

    P.S. Don't try to load the whole file into a variable and send it all at once. First tell the client to open the file, send the file in small chunks like the above sample code, and then tell the client to close the file.

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