Search code examples
javatcpbittorrent

What does BitTorrent and Gnutella uses for bypassing NAT when transferring files?


I´m trying to do a p2p file sharing proto/software in Java. I am interested in learning using socket so, no I will not use JXTA or any kind of API.

I read about TCP hole punching, and it may not work on all NAT types. But I can see that Gnutella and BitTorrent always works on every machine/NAT I try. So do they use TCP Hole Punching to initiate a connection between hosts?

Also, a code like this would do the TCP Hole Punching?

    final ServerSocket s = new ServerSocket(7777);
    Thread t = new Thread(new Runnable(){

        public void run() {
            try
            {
                s.accept();
            }
            catch(Exception ex)
            {

            }
        }
    });

    Socket sock = new Socket();
    sock.connect(new InetSocketAddress("IP ADDRESS", 7777), 50000);

Solution

  • After my research I discovered that TCP is not good for bypassing NATs and TCP Hole Punching is not a 100% sucesfull technique.

    The best way is to use UDP and implement a error tolerance layer over it so as it will work like TCP.

    Also there are some APIs like UDT for Java. But I didnt try it yet http://sourceforge.net/projects/udt-java/