views:

3412

answers:

7

Can anyone point me to a tutorial on the best way to open a connection from client to server, read in a binary file and send its contents reliably across the network connection? Even better, is there an open source library that already does this that I could refer to?

Thanks!

+2  A: 

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

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

Corey Goldberg
A: 

How about using HTTP or FTP? They were sort of made for this.

Alex

Alex Fort
A: 

Depending upon where you are sending the file to, you might want to take a look at WebClient.UploadFileAsync and WebClient.UploadFile.

Rob
A: 

I wouldn't use HTTP or FTP, for a single file it's too much overhead and too much to code, especially having a simple TCP server almost already made for you in C#.

Guille
+2  A: 

This depends what you mean by network - if you're copying on a local network you can just use the file copy operations inside System.IO. If you're wanting to send to remote servers I do this using web services. I compress byte arrays and send them over and decompress on the remote side. The byte array is super easy to write back to disk using streams.

I know some people prefer base 64 strings instead of the byte[]. not sure if it matters.

typemismatch
+2  A: 

You should look into binary serialization and sending it over a TCP socket.

Good explanation on 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

Geoffrey Chetwood
A: 

Sockets may be the best route if you're just having to do it over the network. If you use TCP, you get the reliability of communication but take an impact on speed. If you need higher performance, you could try using UDP instead. But the downside to UDP is that packet delivery and order is not guaranteed, so you would need to write all that plumbing yourself.

If you are needing to transfer files over the web itself (programatically, and if you can't use FTP), then a web service approach via MTOM might fit 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 leverage the new Remote Differential Compression feature. This not only does a really good job at compressing a file to minimize network traffic, but is also used directly by DFS replication. Downside (as a .NET developer), it's a COM+ technology.

jolson