Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: how to get any available port

Reply
Thread Tools

Re: how to get any available port

 
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-04-2005
Mohammed Smadi wrote:

> if am using s.bind for a tcp socket. On the client side i dont really
> care which socket i use as long as i get an available socket. Is there a
> funciton or a way to get an available socket?


why are you using bind if you're on the client side?

</F>



 
Reply With Quote
 
 
 
 
ncf
Guest
Posts: n/a
 
      10-04-2005
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:

 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      10-04-2005
On 2005-10-04, ncf <(E-Mail Removed)> 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?

--
Grant Edwards grante Yow! I want you to
at MEMORIZE the collected
visi.com poems of EDNA ST VINCENT
MILLAY... BACKWARDS!!
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      10-04-2005
Grant Edwards <(E-Mail Removed)> writes:
> > 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?


In the nomenclature of some of these applications, that kind of transfer
is called a client to client connection. Both ends are called clients.
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      10-04-2005
On 2005-10-04, Paul Rubin <> wrote:
> Grant Edwards <(E-Mail Removed)> writes:
>> > 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?

>
> In the nomenclature of some of these applications, that kind
> of transfer is called a client to client connection. Both
> ends are called clients.


IIRC, we were talking about TCP sockets. In a TCP connection,
one end is the client (the one that originates the connection),
and the other end is the server (the one that waits for the
connection on a "well-known" port).

The server calls bind()/listen(), and typically the client just
calls connect(). In theory, the client end can call bind() to
control the originating port used for the connection. But,
servers don't care what the orignating port is, so on the
client end bind() is usually skipped since the default
connect() operation will just choose an available port.

--
Grant Edwards grante Yow! It's strange, but I'm
at only TRULY ALIVE when I'm
visi.com covered in POLKA DOTS and
TACO SAUCE...
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      10-04-2005
Grant Edwards <(E-Mail Removed)> writes:
> > In the nomenclature of some of these applications, that kind
> > of transfer is called a client to client connection. Both
> > ends are called clients.

>
> IIRC, we were talking about TCP sockets.


Yes, but if the person was talking about using TCPs sockets in a chat
application, they may have been using chat terminology rather than TCP
terminology. In a typical chat app, a zillion clients all talk
through TCP to a central server, which is like a phone switch. That
lets Alice connect to the server and immediately see if Bob is online
and what Bob's (possibly dynamic) IP address is. If Alice wants to
send Bob a file, she opens a TCP connection directly to Bob. That's
called a client to client connection because Alice and Bob are both
clients.

So, there's a reasonable application for wanting to open a listener
port without binding any specific port number. Alice would just ask
her OS to assign her a port (say it assigns 23789) and listen on it,
instead of having to contend with other apps on the same IP for some
specific port number. Then she'd send a message through the chat
server asking Bob to connect to port 23789 on her machine.

I'm not sure if there's a way to do this. Do you happen to know?
 
Reply With Quote
 
jepler@unpythonic.net
Guest
Posts: n/a
 
      10-04-2005
On Tue, Oct 04, 2005 at 05:19:37PM -0400, Mohammed Smadi wrote:
> what else would you do? I am using examples from the web and they all
> bind to a port at the localhost before connecting to the remote host.

[...]

the web must be stupider than I thought.

Here's how Python's own ftplib connects to an ftp server (dedented for clarity):
try:
self.sock = socket.socket(af, socktype, proto)
self.sock.connect(sa)
except socket.error, msg:
...

there's no need to call bind() before connect.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDQvNKJd01MZaTXX0RAq73AJ0ddKh3RNuHBzD+xzX5sG HrSwErfQCeJbu+
4nwLzIvnMa3huYRdnEgWFnM=
=bKU2
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-04-2005
Mohammed Smadi wrote:

> what else would you do? I am using examples from the web and they all
> bind to a port at the localhost before connecting to the remote host.


pointers, please.

> 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


that's a typical server setup.

> data="HI"
> print data
> s.connect(('192.168.2.13',port))
> s.send(data)


and this is typical client setup.

are you sure you're not cutting and pasting code from different examples?

> any suggestions for alternative implementation?


if you're writing a client, get rid of the setsockopt and bind stuff.

</F>



 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      10-04-2005
On 2005-10-04, Paul Rubin <> wrote:
> Grant Edwards <(E-Mail Removed)> writes:
>> > In the nomenclature of some of these applications, that kind
>> > of transfer is called a client to client connection. Both
>> > ends are called clients.

>>
>> IIRC, we were talking about TCP sockets.


> So, there's a reasonable application for wanting to open a
> listener port without binding any specific port number. Alice
> would just ask her OS to assign her a port (say it assigns
> 23789) and listen on it, instead of having to contend with
> other apps on the same IP for some specific port number. Then
> she'd send a message through the chat server asking Bob to
> connect to port 23789 on her machine.


Ah. Got it.

> I'm not sure if there's a way to do this. Do you happen to
> know?


IIRC, you just call bind() with a port number of zero, and then
use some method-or-other on the bound socket to find out what
port it's bound to.

--
Grant Edwards grante Yow! Life is a POPULARITY
at CONTEST! I'm REFRESHINGLY
visi.com CANDID!!
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-04-2005
Grant Edwards wrote:

> IIRC, you just call bind() with a port number of zero, and then
> use some method-or-other on the bound socket to find out what
> port it's bound to.


>>> s = socket.socket()
>>> s.bind(("", 0))
>>> s.getsockaddr()

("0.0.0.0", 4711)

</F>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
is there any API available to implement Syslog server using Java (to capture all syslog messages - UDP protocol, port 514)? santa19992000@yahoo.com Java 2 06-20-2006 12:54 PM
Re: how to get any available port Mohammed Smadi Python 3 10-04-2005 09:40 PM
how to get any available port Mohammed Smadi Python 0 10-04-2005 06:33 PM



Advertisments