What is the best way to send a file over the network using C #?

Can someone point me to a tutorial on the best way to open a client connection to a server, read in a binary file and reliably send its contents over a network connection? Even better, is there an open source library that is already doing this, which can I reference?

+6
c # ftp tcp data-transfer
source share
7 answers

You should study binary serialization and send it through a TCP socket.

Good explanation on the different types of serialization:

http://www.dotnetspider.com/resources/408-XML-serialization-Binary-serialization.aspx

Good primer on TCP Client / Server in C #:

http://www.codeproject.com/KB/IP/tcpclientserver.aspx

+3
source share

You can make a TCP client and server and send messages over TCP. Its pretty trivial in C #.

Here is an example of a simple TCP server:
http://www.goldb.org/goldblog/2007/07/27/CSimpleTCPServer.aspx

+2
source share

It depends on what you mean by the network - if you are copying on a local network, you can simply use the file copy operation inside System.IO. If you want to send to remote servers, I do this using web services. I compress byte arrays and send them and unpack them on the far side. The byte array is super easy to write back to disk using streams.

I know that some people prefer base 64 lines instead of byte []. not sure if that matters.

+2
source share

How to use HTTP or FTP? They were made for this.

Alex

0
source share

Depending on where you are sending the file, you may have a look at WebClient.UploadFileAsync and WebClient.UploadFile .

0
source share

I would not use HTTP or FTP, for one file it is too much overhead and too much for code, especially having a simple TCP server already made for you in C #.

0
source share

Sockets can be the best route if you just need to do this over the network. If you use TCP, you get reliable communications, but have an effect on speed. If you need better performance, you can try using UDP instead. But the disadvantage of UDP is that delivery and ordering packages are not guaranteed, so you will need to write everything you need for plumbing.

If you need to transfer files through a website (programmatically, and if you cannot use FTP), then the web service approach through MTOM may suit your needs.

If you are building on top of Windows Server 2003 R2, Windows Vista, or Windows Server 2008 and doing internal network transfers, another option is to use the new Remote Differential Compression . This not only does a good job of compressing the file to minimize network traffic, but it is also directly used by DFS replication. Downside (as a .NET developer) is COM + technology.

0
source share

All Articles