how to get any available port

Grant Edwards grante at visi.com
Tue Oct 4 17:38:02 EDT 2005


On 2005-10-04, Mohammed Smadi <smadim2 at grads.ece.mcmaster.ca> wrote:
> On Tue, 4 Oct 2005, Grant Edwards wrote:
>
>> On 2005-10-04, ncf <nothingcanfulfill at gmail.com> wrote:
>> 
>> > Hmm...perhaps he is trying to do a transfer thing like many chat
>> > programs do. Instead of sending large files across a server, you
>> > "Direct Connect" and send the file directly. :shrugs:
>> 
>> So how does that require binding the client end of a TCP
>> connection?
>
> what else would you do?

Just call connect():

> I am using examples from the web and they all bind to a port
> at the localhost before connecting to the remote host.

I don't know who wrote those examles, but I've never seen it
done that way before.  Take a look at the examples from the
python socket module docs:

http://docs.python.org/lib/socket-example.html

> my code is like this
>
> #transmission socket
> s = socket.socket(socket.AF_INET,  socket.SOCK_STREAM)
> s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
> s.bind(("",hp_port)) # do some error checking
>
>
> data="HI"
> print data
> s.connect(('192.168.2.13',port))
> s.send(data)
> print "\n"
>
> which is working fine for me after putting the 
> s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) statement

Just skip the bind() call.  It's useless.

#transmission socket

s = socket.socket(socket.AF_INET,  socket.SOCK_STREAM)
s.connect(('192.168.2.13',port))
s.send(data)

-- 
Grant Edwards                   grante             Yow!  Are you still an
                                  at               ALCOHOLIC?
                               visi.com            



More information about the Python-list mailing list