I am trying to build an application that can request files from a service running on another machine in the network. These files can be fairly large (500mb + at times). I was looking into sending it via TCP but I'm worried that it may require that the entire file be stored in memory.
There will probably only be one client. Copying to a shared directory isn't acceptable either. The only communication required is for the client to say "gimme xyz" and the server to send it (and whatever it takes to ensure this happens correctly).
Any suggestions?
Use
TransmitFile
(which is a Win32 function; perhaps it's a method of the .NET library as well).Personally I'd go for something that balances speed, reliability and economical code, so I'd base it on a TCP network stream. The client-side of the code would look like this:
Then have a server class to serve the file. Note how the whole file isn't loaded into memory, instead it is read in chunks from a
FileStream
.The TcpClientWrapper is pretty much boiler plate code with the
System.Net.Sockets.TcpClient
object and the underlyingNetworkStream
object. I don't really need to post this as well, but just to give some pointers ther construction would contain something like this:and the
DataReceivedAsync
method is boilerplate socket data handling and would raise an event o share the received data back to the consumer (the client in this case):The event to ship data from the wrapper back to the client:
Be careful with BITS. It is a very good protocol but not a critical part of the windows update program. We found that hardly any of our corporate clients allowed the BITS update onto their machines; therefore we couldn't build an app that relied on it.
Use FTP via the open source edtFTPnet library. Fast and simple.
If FTP was an option then I'd go with that for the sake of simplicity. Otherwise you're into a world of TCP/IP socket programming.
Here is an easier way. Using BITS (Background Intelligent Transfer Service). Its already built into WinXP and Vista. Its basically what drives Windows Updates.
http://blogs.msdn.com/powershell/archive/2009/01/11/transferring-large-files-using-bits.aspx
http://blogs.msdn.com/jamesfi/archive/2006/12/23/how-to-use-bits-to-transfer-files.aspx
Here is a nice managed BITS wrapper someone wrote and how to use it.
http://www.codeproject.com/KB/cs/Managed_BITS.aspx