Jump to content

UDP Connections


Recommended Posts

I am making a P2P chat program, but am having a few issues:

(I am pretty new to networking so please bear with me)

I can get the connection to work, but only if I port forward the computers. I could solve this with TCP and using a central server, but I want it to not require any configuration for either user and still have it be Peer to Peer. I came across this blog post (http://leadthinking.com/213-p2p-using-tcp-ruby) talking about TCP p2p. At the beginning he describes how UDP p2p works, and then TCP later on. For now, I just want to stick to UDP because in there he uses other protocols for TCP.

From the blog post:

With UDP it’s fairly straight forward – that’s why it’s so commonly used in P2P software.

Client A sends a packet to Client B, opening a hole in Client A’s firewall. That packet will be discarded by Client B’s firewall, but that doesn’t matter. Client B then does the reverse, sending a packet back Client A – now there’s a hole in both firewalls and a P2P connection. I’m glossing over some of the more technical details, such as STUN and port prediction, but that’s the general gist. Here’s a good article on how Skype uses UDP P2P techniques to traverse firewalls.

Is there something I am not understanding? How would I do that in AutoIt? I edited the UDPSend.au3 example like this:

; Start The UDP Services
;==============================================
UDPStartup()

; Register the cleanup function.
OnAutoItExitRegister("Cleanup")

$myip = "XXX.XXX.XXX.XXX";censored
;==============================================
$socket_out = UDPOpen($myip, 65531);I have another script with the ports flipped, decided not to post that
$socket_in = UDPBind(@IPAddress1, 65532)
If @error <> 0 Then Exit

$n=0
While 1
    $data = UDPRecv($socket_in, 50)
    If $data <> "" Then
        MsgBox(0, "UDP DATA", $data, 1)
    EndIf
    Sleep(2000)
    $n = $n + 1
    $status = UDPSend($socket_out, "Message #" & $n)
    If $status = 0 then
        MsgBox(0, "ERROR", "Error while sending UDP message: " & @error)
        Exit
    EndIf
WEnd

Func Cleanup()
    ;UDPCloseSocket($socket)
    UDPShutdown()
EndFunc

(I have another script with the ports flipped, decided not to post that)

I attempted to recreate his diagram, but the script doesn't work... Can any one help me out?

(The above script doesn't work without port forwarding)

Edited by clonetrooper9494
Link to comment
Share on other sites

I'm sorry to tell you this but I don't think it'll work.... here is why:

Your home/corp firewall uses PNAT (I think) which translates both your source IP and your source port. So when you send a packet with UDP SRC PORT 1234 your firewall will take that port and map it to a random port number (i.e. 12042)

So you'll need a central server which knows which src port number your firewalls decided to use for the current connection.

Read this:How Skype Does it

I might be wrong... but I don't think so :mellow:

[EDIT]: The term is Network Address/Port Translation (NAPT) not PNAT

Also I don't think this guy at leadthinking.com knows his stuff and the proof is that he didn't supply a working code. Read this for more details too:

http://www.bford.info/pub/net/p2pnat/

Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

I see now, but I don't think I want to spend the time developing hole-punching software in AutoIt... I guess I will have to stick to central servers for my chat.

But you must understand that this P2P solution was developed to take the load off a central server.... Come on! you've gotta at least give it a shot!

Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

  • 5 years later...

Almost 6 years have passed since this talk. I wonder, has anything changed in this direction. I am using tcp for communication between 2 pcs, when the 2ip addresses are constant. Is there a solution since that time? Sorry for necroposting

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...