I need to send a file to a user behind a router. I know I can do this with a tcp connection if he enables port forwarding on his router. I want to avoid this in order to make my application easier to use for my users. So I been trying to do tcp punch holing and that is almost impossible. I managed to do udp puch holing.
if you already know what udp punch holing is, then you may skip this part:
so what I did in order to establish a connection between two users that happen to be behind a router was. user 1 talks to a server that has a WAN ip address via the udp protocol. user 2 also does the same thing. ok so now the server knows the remote end point of each user. the server now sends use开发者_如何学编程r 2 the info of user 1. server notifies user 1 that user 2 has received the info. user 2 then sends packages to the router ip address of user 1. the packages received by that router get forwarded to user 1 because they come from the port specified by the server. so know all the packages that user 2 sends to the router of user 1 will get to user 1 if they are sent through the port that was specified by the server. when the packages arrive to user 1 those packages arrive with user 2 info. user 1 now know how to send data two user 2 as well.
So now I am able to send data between two users that happen to be behind distinct routers. The reason why I used the udp protocol was because it is similar to tcp when using c#. I been trying to do this with tcp but I just cannot make it work. this application that I am crating is written in c# and everything works great when users happen to be behind the same router. but know I want to extend the functionality.
So now I finally got my first step on connecting two clients behind a different nat. the only problem is that the only way I managed to do this was with the upd protocol. I been reading that upd does not ensure the delivery of data and also that data may be received on a different order.
Maybe I can create an algorithm in order to be able to send data via udp knowing that data will not be lost.
another alternative is to use a different protocol such as ftp, bittorrent... etc. I don't know if it will be possible to do ftp punch holing though.
so in short I just want to be able to enable two peers that are behind distinct routers to be able to communicate with the help of a server. I do not want to communicate through ralying (connection goes from client A to server to client B) because sometimes it get's difficult to manage and it is expensive to maintain when a lot of users use it. Maybe I should use a different protocol such as a bittorrent library and I haven't done that because I haven't find a good example on the internet that shows you how to use that library.
http://nutss.gforge.cis.cornell.edu/stunt.php
It's caller STUNT.
Simple Traversal of UDP Through NATs and TCP too (STUNT), which extends STUN to include TCP functionality, is a lightweight protocol that allows applications running behind a NAT to determine external IP and port-binding properties, packet filtering rules and various timeouts associated with TCP connections through the NAT. Knowing these parameters allows applications to establish TCP sessions between two NAT'ed hosts. As a result P2P and other applications can work through existing NAT infrastructure without sacrificing the benefits of TCP.
UPnP might also be an option.
精彩评论