PDA

Click to See Complete Forum and Search --> : transfer file


Sep 14th, 2000, 07:45 AM
How i receive a big binary file (mp3 files with 3 or 4 mb) using winsock?

I use getdata, and, at the DataArrival event, i write the data receiver in a file in disk. but at the finish.. the file dont work ( corrupt? )

how the right mode to receive a big binary file (peekdata or getdata)?
how the right mode to write this file in disk?

thx for all.

[]s Claudio Castro

Sep 15th, 2000, 10:01 PM
This is a common problem that people have when they use Winsock to transfer large files.

For example, take a 3MB MP3 file, when you have connected and you use SendData, Winsock doesn't send it all at once, it will break it up into multiple pieces and sends them one by one. So at the receiving end, the DataArrival event will fire everytime a new packet comes in, which means that if you are using:


Winsock1.GetData Data


the 'data' from the last packet will be overwritten by the new incoming one.

One way I use to solve this is to use the bytesTotal value in the DataArrival event. bytesTotal is a value that can be set, and when the number of bytes of incoming data matches that of bytesTotal's value, Winsock will stop receiving data. This is commonly called the chunksize. Thus, by setting the 'chunksize' to a value smaller than that of one of Winsock's packets, and repeatedly send packets of that size, your packets won't be broken up by Winsock.

A good way to set bytesTotal is to send a header to the receiving end before you start to send the file. The header should contain things like the chunksize, filesize (so you can compare the original filesize and the filesize of the received file) and probably the sender and the filename. The header should use fixed length variables, this will make it easier to parse.

Eg:

Dim Sender As String
Dim Chunk As String * 4

Sender = Winsock1.LocalHostName
Chunk = "1000"


Thus, your header will look like:
1000Name

At the receiving end, you should Left(Data, 4) to see if the first 4 characters is the chunk size, then if it is


Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim size As Integer

Winsock1.GetData Data

If Val(Left(Data, 4)) <> 0 'means it is a number
bytesTotal= Left(Data, 4)
End if


And only after receiving the header should the sending end send the proper chunks of the file.

However, you should understand that the code here is very simple and only shows the concept behind bytesTotal and header sending. Your proper code will undoubfully end up to be much more complicated in order suit what you want it to do and how you want to send/receive/process the header and file.

If you want to, I can go through the concept is greater detail (and how I actually like to do things with Winsock)

Sunny

Sep 18th, 2000, 01:53 PM
Can you send me some of your examples of using the winsock
sunnyl? cause i want to learn how to use "SOCK" pretty good.

send to jeffboddie@aol.com

Thanks .