-
Feb 9th, 2001, 07:15 PM
#1
Thread Starter
Member
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 ::.
-
Feb 9th, 2001, 11:43 PM
#2
PowerPoster
why not use TCP/IP protocol?
-
Feb 9th, 2001, 11:46 PM
#3
Thread Starter
Member
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 ::.
-
Feb 9th, 2001, 11:51 PM
#4
PowerPoster
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
-
Feb 9th, 2001, 11:52 PM
#5
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.
-
Feb 9th, 2001, 11:59 PM
#6
Thread Starter
Member
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 ::.
-
Feb 10th, 2001, 12:05 AM
#7
PowerPoster
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
-
Feb 10th, 2001, 12:10 AM
#8
Thread Starter
Member
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 ::.
-
Feb 10th, 2001, 12:29 AM
#9
Thread Starter
Member
hmmm..
now im getting ano overflow..
Open dlg.FileName For Binary Access Read As #1
fsize = LOF(1) ' <-- overflow?
Close #1
.:: jier :: 545 Studios ::.
-
Feb 10th, 2001, 01:08 AM
#10
PowerPoster
Make sure you've declare the fsize to long datatype.
-
Feb 10th, 2001, 01:11 AM
#11
Thread Starter
Member
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 ::.
-
Feb 10th, 2001, 01:23 AM
#12
PowerPoster
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
-
Feb 10th, 2001, 01:27 AM
#13
Thread Starter
Member
thanks
lemme try to fix up my code by looking at yours..i hope it works!
.:: jier :: 545 Studios ::.
-
Feb 10th, 2001, 02:20 AM
#14
Thread Starter
Member
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 ::.
-
Feb 10th, 2001, 02:42 AM
#15
PowerPoster
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
<------
-
Feb 10th, 2001, 02:46 AM
#16
Thread Starter
Member
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 ::.
-
Feb 10th, 2001, 02:52 AM
#17
PowerPoster
jier, you can save the receive data into the file at file or block by block.
or perhaps you can post your code?
-
Feb 10th, 2001, 02:57 AM
#18
Thread Starter
Member
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 ::.
-
Feb 10th, 2001, 03:00 AM
#19
Thread Starter
Member
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 ::.
-
Feb 10th, 2001, 03:10 AM
#20
PowerPoster
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.
-
Feb 10th, 2001, 03:13 AM
#21
Thread Starter
Member
.:: jier :: 545 Studios ::.
-
Feb 10th, 2001, 03:22 AM
#22
PowerPoster
-
Feb 10th, 2001, 03:24 AM
#23
Thread Starter
Member
yeah..
i didnt know there was any other way to do it.
.:: jier :: 545 Studios ::.
-
Feb 10th, 2001, 03:33 AM
#24
PowerPoster
did you try out my method?
-
Feb 10th, 2001, 03:38 AM
#25
Thread Starter
Member
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 ::.
-
Feb 10th, 2001, 03:45 AM
#26
PowerPoster
what do you mean "<- returns null "
Are you encounter error in that line of code?
-
Feb 10th, 2001, 03:52 AM
#27
Thread Starter
Member
no, tmp returns nothing.. tmp = ""
.:: jier :: 545 Studios ::.
-
Feb 10th, 2001, 04:03 AM
#28
PowerPoster
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
-
Feb 10th, 2001, 04:21 AM
#29
Thread Starter
Member
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 ::.
-
Feb 10th, 2001, 06:10 AM
#30
PowerPoster
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?
-
Feb 10th, 2001, 06:13 AM
#31
Addicted Member
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
-
Feb 10th, 2001, 06:13 AM
#32
Thread Starter
Member
i close 1, just incase it was left open...the data is whatever the user sends, zip, bmp, txt, whatever
.:: jier :: 545 Studios ::.
-
Feb 10th, 2001, 06:20 AM
#33
PowerPoster
jier, try to change the Space$ to String as show below:
Code:
tmp = Space$(bSz)
tmp = String(bSz, Chr(0))
-
Feb 10th, 2001, 06:22 AM
#34
PowerPoster
Also, please ensure you have passing the fullpath of the file that you try to read, like
FileToSend = "C:\...\Mydata.txt
-
Feb 10th, 2001, 06:34 AM
#35
Thread Starter
Member
tried it..no luck.. hmmmmm
this is turning into a major task
.:: jier :: 545 Studios ::.
-
Feb 10th, 2001, 06:43 AM
#36
PowerPoster
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
-
Feb 10th, 2001, 06:59 AM
#37
Thread Starter
Member
Well, That worked!...so, can you explain why it was doing that?
.:: jier :: 545 Studios ::.
-
Feb 10th, 2001, 07:09 AM
#38
PowerPoster
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.
-
Oct 30th, 2001, 04:35 AM
#39
Retired VBF Adm1nistrator
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|