[BangPypers] UDP Hole Punching using python
kunal.t2 at gmail.com
Sat Feb 26 05:14:42 CET 2011
On Sat, Feb 26, 2011 at 8:09 AM, Senthil Kumaran <orsenthil at gmail.com>wrote:
> On Sat, Feb 26, 2011 at 06:48:51AM +0530, kunal ghosh wrote:
> > On Sat, Feb 26, 2011 at 6:37 AM, Senthil Kumaran <orsenthil at gmail.com
> > > On Fri, Feb 25, 2011 at 08:42:31PM +0530, kunal ghosh wrote:
> > > > step 1: holepunch , send UDP packet from receiver to sender
> > > > from socket import *
> > > > s = socket(AF_INET,SOCK_DGRAM)
> > > > host = "<receiver ip>"
> > > > port = <receiver port>
> > > > s.bind((host,port))
> > > > s.sendto("Message",("<sender ip>",<sender port>))
> > > >
> > > > here i get the following error
> > > > *error: [Errno 22] Invalid argument*
> > > > i dunno how to resolve this issue.
> > >
> > > Don't use the bind call in the UDP client.Make sure your ipaddress is
> > > proper.Otherwise your code looks fine.
> > >
> > I am using the bind call to make sure i use a specific source port to
> > the message.
> Could you try that and the suggestion work?
> BTW, there is "No Harm" in actually binding to a port while sending.
> > Is there any other way that can be achieved ?
> s.bind(("",55447) # Ask it to use any interface by using ""
Thanks a lot senthil, binding the socket to "" (any interface) worked :) Yay
If i try to bind to something like localhost, i get the error while sending
the packet .
Now i am able to send UDP packets to and from two computers hidden behind
their NATs :)
> So, I think your original problem was with some invalid values.
> > How can the sender know which port was used to send the packet , after
> > packet was sent ?
> After you do the sendto call, you can get the sender's socket
> information port by
> BangPypers mailing list
> BangPypers at python.org
Dept of Computer Sc. & Engineering.
More information about the BangPypers