P2P File Transfer over TCP

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.

Untitled 2


  1. Sender obtains IP Address of receiving client and the file to send to him.
  2. 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.
  3. The sender sends the file information (file name + file size) to the receiver over the established connection in part 2.
  4. 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).
  5. 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.
  6. Sender closes the connection.
  7. File transfer complete.


  1. Receiving client listens to TCP port 1723 for incoming connection.
  2. Client establishes a connection with sender when sender attempts to do so. (Sender part 2)
  3. Client reads file information (file name + file size) sent over by sender from the connection’s stream.
  4. Client prompts user to accept or reject the file transfer.
  5. If user accepts, client prompts user for the location to save the received file.
  6. 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).
  7. 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.
  8. Client closes the connection.
  9. File transfer complete.

Demo program

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.


10 thoughts on “P2P File Transfer over TCP

  1. Hi Lee,
    I have tested you program over internet to transfer files. but it is not working. It will never work when both Machines are behind the NAT. means if machines are connected to internet using Routers then without port forwarding it is not possible. AM I Right?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s