|
-
Apr 18th, 2001, 12:59 PM
#1
Thread Starter
New Member
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,
-
Apr 18th, 2001, 08:42 PM
#2
Member
-
Apr 19th, 2001, 06:16 PM
#3
Thread Starter
New Member
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,
-
Apr 30th, 2001, 04:48 PM
#4
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|