File Transfer for VERY large files

kantplayalik

Member
Joined
Mar 17, 2006
Messages
7
Programming Experience
1-3
Hello all, I find myself faced with a problem. I'm writing a vb,net application (2005) to do a one way file transfer. I've done this kind of thing before without too many headaches at work. However, I'm going to be sending VERY large files upwards of 5 Gigs and the code I have won't handle it...I keep getting an out of memory exception. Here is a snippet.


VB.NET:
Dim fileLength As Long = fi.Length 
Dim fs As New FileStream(filename, FileMode.Open, FileAccess.Read) 
Dim bytesWritten As Integer = 0 
Dim bytesdone As Integer = 0 
Dim fileData(1024 * 1024) As Byte 
Dim tmp As Integer = 0 
Do 
bytesWritten = fs.Read(fileData, 0, fileData.Length) 
If bytesWritten > 0 Then 
If bytesWritten >= fileData.Length And (fileLength - bytesdone) >= fileData.Length Then 
tmp = fileData.Length 
ElseIf bytesWritten < fileData.Length And (fileLength - bytesdone) >= fileData.Length Then 
tmp = bytesWritten 
Else 
tmp = Convert.ToInt32((fileLength - bytesdone)) 
End If 
End If 
ns.Write(fileData, 0, tmp) 
bytesdone += tmp 
Console.WriteLine(Convert.ToString(bytesdone) & " Of " & size) 
Loop While (bytesdone < fileLength)
Any ideas on what I should do differently ?

Thanks in advance for your repiles/comments. :)
 
Last edited by a moderator:
In code box is the loop I use for large files, simpler equations than you use anyway. I haven't tested a 5GB file but I sent a continuous stream of five 700MB files through without problem. One thing you should think about with that large 1MB filebuffer you use is that the networkstream have to chop it according to the systems DefaultSendWindow setting. I set the filebuffer equal to this. My internet broadband optimized DefaultSendWindow size is currently around 65000 bytes.
VB.NET:
Dim filebyteswritten As Long = 0
Dim filebytesread As Integer = 0
Dim filebuffer(tcp.SendBufferSize - 1) As Byte
Dim filestream As New IO.FileStream(filename, IO.FileMode.Open, IO.FileAccess.Read)
Do Until filebyteswritten = fileLength
  filebytesread = filestream.Read(filebuffer, 0, filebuffer.Length)
  netstream.Write(filebuffer, 0, filebytesread)
  filebyteswritten += filebytesread
Loop
filestream.Close()
 
Back
Top