Re: how to get any available port

Discussion in 'Python' started by Fredrik Lundh, Oct 4, 2005.

  1. 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>
     
    Fredrik Lundh, Oct 4, 2005
    #1
    1. Advertising

  2. Fredrik Lundh

    ncf Guest

    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:
     
    ncf, Oct 4, 2005
    #2
    1. Advertising

  3. On 2005-10-04, ncf <> 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!!
     
    Grant Edwards, Oct 4, 2005
    #3
  4. Fredrik Lundh

    Paul Rubin Guest

    Grant Edwards <> 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.
     
    Paul Rubin, Oct 4, 2005
    #4
  5. On 2005-10-04, Paul Rubin <> wrote:
    > Grant Edwards <> 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...
     
    Grant Edwards, Oct 4, 2005
    #5
  6. Fredrik Lundh

    Paul Rubin Guest

    Grant Edwards <> 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?
     
    Paul Rubin, Oct 4, 2005
    #6
  7. Fredrik Lundh

    Guest

    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+xzX5sGHrSwErfQCeJbu+
    4nwLzIvnMa3huYRdnEgWFnM=
    =bKU2
    -----END PGP SIGNATURE-----
     
    , Oct 4, 2005
    #7
  8. 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>
     
    Fredrik Lundh, Oct 4, 2005
    #8
  9. On 2005-10-04, Paul Rubin <> wrote:
    > Grant Edwards <> 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!!
     
    Grant Edwards, Oct 4, 2005
    #9
  10. 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>
     
    Fredrik Lundh, Oct 4, 2005
    #10
  11. Fredrik Lundh

    Paul Rubin Guest

    "Fredrik Lundh" <> writes:
    > 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)


    Nice. Thanks.
     
    Paul Rubin, Oct 4, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John T. Goodman

    Overhead of 4-port over 2-port SRAM

    John T. Goodman, Jan 25, 2005, in forum: VHDL
    Replies:
    0
    Views:
    608
    John T. Goodman
    Jan 25, 2005
  2. Sean Wolfe
    Replies:
    1
    Views:
    2,262
    Joerg Jooss
    Apr 28, 2005
  3. b3ny
    Replies:
    11
    Views:
    929
    Babu Kalakrishnan
    Nov 20, 2004
  4. Mohammed Smadi

    how to get any available port

    Mohammed Smadi, Oct 4, 2005, in forum: Python
    Replies:
    0
    Views:
    283
    Mohammed Smadi
    Oct 4, 2005
  5. Mohammed Smadi

    Re: how to get any available port

    Mohammed Smadi, Oct 4, 2005, in forum: Python
    Replies:
    3
    Views:
    384
    Grant Edwards
    Oct 4, 2005
Loading...

Share This Page