File transfers usually take place via an intermediate server – Your files get uploaded to the intermediate server, and others download from the intermediate server instead of from you. On the other hand, in another perhaps less well-known class of file transfers, Peer-To-Peer (P2P) file transfers, the files get sent directly from you to the client without going through any intermediate servers.
Today, we’ll be looking at how to do P2P file transfers over TCP (http://en.wikipedia.org/wiki/Transmission_Control_Protocol). There will be two parts – sending and receiving, both of which can be summarized (together) with the following overview.
- Sender obtains IP Address of receiving client and the file to send to him.
- Sender then attempts to establish a connection to the obtained IP Address over TCP using a constant port – we will use port 1723 (some random number) for illustration purposes.
- The sender sends the file information (file name + file size) to the receiver over the established connection in part 2.
- Sender waits for client to grant permission for the incoming file request, by reading a single byte from the connection’s stream. (0 = denied, 1 = accepted).
- If permission has been granted, sender writes the contents of the file to the connection’s stream, I.E. sends the file to the client.
- Sender closes the connection.
- File transfer complete.
- Receiving client listens to TCP port 1723 for incoming connection.
- Client establishes a connection with sender when sender attempts to do so. (Sender part 2)
- Client reads file information (file name + file size) sent over by sender from the connection’s stream.
- Client prompts user to accept or reject the file transfer.
- If user accepts, client prompts user for the location to save the received file.
- Client writes back a single byte to the connection’s stream to notify the sender if permission has been granted or not. (0 = denied, 1 = accepted).
- Client starts reading the contents of the file from the connection’s stream, and writes it to the file which the user has picked in part 5 to save the file. I.E. Client receives the file.
- Client closes the connection.
- File transfer complete.
I’ve written up a program in C# to demonstrate our implementation of P2P file transfers over TCP. I’ve choose to target .NET Framework 4.5, because of it’s awesome (it really is!) asynchronous capabilities. Visual Studio 2012 is required to open up and to compile the project.
Project download link: https://hostr.co/LY24naXAEYLp
As we’ve seen above, P2P file transfers are rather easy to implement, along with several other advantages. You won’t need an intermediate server to store your files for clients to download from, and this can be a big plus since it’s hard (almost impossible) to find free online file storage servers to upload large files (> 5 GB for example) to.
Furthermore, because your file transfer takes place without an intermediate server, it is now harder for people *cough* governments *cough* to track what you are sharing or to hijack your files.
However, if the sender wishes to send the file more than once, he would have to upload the file again and again, once for each client, and this of course puts a strain on your upload bandwidth. It is hence not advisable to send large files through P2P file transfers (though it is worth mentioning that large file transfers over P2P has been largely successful with the BitTorrent distribution system, but that’s slightly different from our case).
Furthermore, the TCP port (1723) has to be opened explicitly for the client, leading to more trouble (unless you are using UPnP, NAT-PMP, etc port mappings) and perhaps even security issues.
With that, P2P file transfers are merely alternatives to the mainstream form of file transfers. It is just another tool, now in your arsenal, that should be used when tackling certain problems. Of course, you should not be using it when you don’t have to or shouldn’t have to, similar to all other programming tools.