Ports capable of multiple simultaneous connections?

Discussion in 'Java' started by sierra1bravo@gmail.com, Oct 18, 2005.

  1. Guest

    Dear all
    Although there are several postings that discuss this topic, I am
    unable to form a conclusive position on this topic: can a given local
    port connect simultaneously to many remote sockets?

    ---------
    ServerSocket ss = new ServerSocket(1234);

    do {
    Socket s = ss.accept();
    System.out.println("Local port is: "+s.getLocalPort());
    Thread t = new MyHandlerThread(s);
    } while (true);
    ------------

    Now, if there are several simultaneous connections, all of them show
    that the local port is 1234. In addition to all these, there is also
    the listening port which open at the same number as well.

    I understand that accept() returns a new Socket each time for every new
    connection, and I assumed implicitly that each new Socket uses a new
    (temporary) port. But that appears false--all sockets returned use the
    same port (and this looks counterintuitive for me).

    So, are ports capable of multiple simultaneous connections, or am I
    going wrong somewhere?

    TIA

    Sierra B.
     
    , Oct 18, 2005
    #1
    1. Advertising

  2. On 18 Oct 2005 02:19:50 -0700, wrote:
    > Although there are several postings that discuss this topic, I am
    > unable to form a conclusive position on this topic: can a given local
    > port connect simultaneously to many remote sockets?


    Yes.

    > ---------
    > ServerSocket ss = new ServerSocket(1234);
    >
    > do {
    > Socket s = ss.accept();
    > System.out.println("Local port is: "+s.getLocalPort());
    > Thread t = new MyHandlerThread(s);
    > } while (true);
    > ------------
    >
    > Now, if there are several simultaneous connections, all of them show
    > that the local port is 1234. In addition to all these, there is also
    > the listening port which open at the same number as well.
    >
    > I understand that accept() returns a new Socket each time for every new
    > connection, and I assumed implicitly that each new Socket uses a new
    > (temporary) port. But that appears false--all sockets returned use the
    > same port (and this looks counterintuitive for me).
    >
    > So, are ports capable of multiple simultaneous connections, or am I
    > going wrong somewhere?


    Your observation is correct. Your intuition is not. There is no
    temporary port involved (a common misconception).

    The port number simply represents an application or service on that
    particular host. It doesn't represent a socket (a connection
    endpoint), and certainly not a connection (a socket pair).

    Note that a socket is defined as a port number and an address. The
    listening socket (ServerSocket in Java terms) has the port you've
    chosen and (normally) a wildcard local address.

    Each connected socket inherits its port number from the listening
    socket, and its local address is bound to the address connected to by
    the client. The connection itself is unique since the other half of
    its identity comes from the connecting socket's address and port
    number.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Oct 18, 2005
    #2
    1. Advertising

  3. Sierra Bravo Guest

    Interesting...I thought the idea of a port was to disambiguate between
    multiple destinations for a packet in a single machine.

    In other words, IP disambiguates the node (among other nodes in the
    network), but IP is not sufficient when there are multiple processes
    that can receive packets in any given machine (eg, a machine that has
    processes listening on port 25, 80 and 110). It is the port that
    identifies the ultimate destination.

    However if a port is attached to multiple threads as in this case, how
    does the disambiguation work? Is there a still lower-level mechanism
    that is used to do this?

    best

    satish


    Gordon Beaton wrote:

    > Your observation is correct. Your intuition is not. There is no
    > temporary port involved (a common misconception).
    >
    > The port number simply represents an application or service on that
    > particular host. It doesn't represent a socket (a connection
    > endpoint), and certainly not a connection (a socket pair).
    >
    > Note that a socket is defined as a port number and an address. The
    > listening socket (ServerSocket in Java terms) has the port you've
    > chosen and (normally) a wildcard local address.
    >
    > Each connected socket inherits its port number from the listening
    > socket, and its local address is bound to the address connected to by
    > the client. The connection itself is unique since the other half of
    > its identity comes from the connecting socket's address and port
    > number.
    >
    > /gordon
    >
    > --
    > [ do not email me copies of your followups ]
    > g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Sierra Bravo, Oct 18, 2005
    #3
  4. On 18 Oct 2005 04:10:50 -0700, Sierra Bravo wrote:
    > Interesting...I thought the idea of a port was to disambiguate
    > between multiple destinations for a packet in a single machine.
    >
    > In other words, IP disambiguates the node (among other nodes in the
    > network), but IP is not sufficient when there are multiple processes
    > that can receive packets in any given machine (eg, a machine that has
    > processes listening on port 25, 80 and 110). It is the port that
    > identifies the ultimate destination.


    The port number is only one of several attributes used to identify the
    connection.

    > However if a port is attached to multiple threads as in this case,
    > how does the disambiguation work? Is there a still lower-level
    > mechanism that is used to do this?


    Port numbers are defined by TCP (and UDP). IP does not know about port
    numbers, it only knows about IP addresses.

    IP passes incoming packets to TCP (or UDP), where the recipient is
    determined based on this 4-tuple:

    { local address, local port, remote address, remote port }

    Any two connections will differ in at least one of the four fields, so
    they are distinct and there is no ambiguity. TCP must examine all four
    fields in order to dispatch incoming packets to the right destination.

    A "connection" for a typical ServerSocket might look like this:

    { *, 80, *, * }

    where * represent any address or port number.

    When a client connects, the listening socket is cloned and the
    wildcard fields are bound, resulting in a connection that looks like
    this:

    { Server_IP, 80, HostA_IP, Client1_Port }

    A second client connecting from the same host would result in this
    connection:

    { Server_IP, 80, HostA_IP, Client2_Port }

    A third client connecting, this time from a different host:

    { Server_IP, 80, HostB_IP, Client3_Port }

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Oct 18, 2005
    #4
  5. "Sierra Bravo" <> wrote in message
    news:...
    > Interesting...I thought the idea of a port was to disambiguate between
    > multiple destinations for a packet in a single machine.
    >
    > In other words, IP disambiguates the node (among other nodes in the
    > network), but IP is not sufficient when there are multiple processes
    > that can receive packets in any given machine (eg, a machine that has
    > processes listening on port 25, 80 and 110). It is the port that
    > identifies the ultimate destination.
    >
    > However if a port is attached to multiple threads as in this case, how
    > does the disambiguation work? Is there a still lower-level mechanism
    > that is used to do this?


    This really doesn't have anything to do with threads although it is common
    (but not required) for each connection to be serviced by its own thread. The
    full connection between one machine and another is uniquely identified by
    the client IP, client port, server IP and server port. Multiple connections
    to the same service (server IP & port) are disambiguated by the fact that
    the come from different clients (client IP-port pairs) Each connection
    end-point receives its own stream from its corresponding client.

    Cheers,
     
    Matt Humphrey, Oct 18, 2005
    #5
  6. Sierra Bravo wrote:
    > Interesting...I thought the idea of a port was to disambiguate between
    > multiple destinations for a packet in a single machine.
    >
    > In other words, IP disambiguates the node (among other nodes in the
    > network),


    Not exactly. IP is not centered around hosts or nodes, but interfaces.
    The host ID part of an IP address does not address that particular host
    as a whole, but an IP interface at that host. A single machine can have
    multiple interfaces, each with an own IP address.

    > but IP is not sufficient when there are multiple processes


    It doesn't have to be multiple processes. A single process can listen to
    multiple ports. The common usage for ports is to differentiate between
    services - services which can, but don't need to, be provided by
    different processes.

    > that can receive packets in any given machine (eg, a machine that has
    > processes listening on port 25, 80 and 110). It is the port that
    > identifies the ultimate destination.


    No, it is the so called "half association", aka "socket", aka "transport
    address" which determines the ultimate destination.

    A "half association" consists of

    - protocol designation (e.g. TCP or UDP)
    - IP address (to identify the host's interface)
    - port

    The port alone is not sufficient. E.g. TCP port 1234 and UDP port 1234
    are different ports. And TCP port 9876 on the host's IP address
    192.168.1.1 is different from TCP port 9876 on the host's IP address
    192.168.1.2.

    /Thomas
    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
     
    Thomas Weidenfeller, Oct 18, 2005
    #6
  7. Alan Krueger Guest

    Sierra Bravo wrote:
    > Interesting...I thought the idea of a port was to disambiguate between
    > multiple destinations for a packet in a single machine.
    >
    > In other words, IP disambiguates the node (among other nodes in the
    > network), but IP is not sufficient when there are multiple processes
    > that can receive packets in any given machine (eg, a machine that has
    > processes listening on port 25, 80 and 110). It is the port that
    > identifies the ultimate destination.
    >
    > However if a port is attached to multiple threads as in this case, how
    > does the disambiguation work? Is there a still lower-level mechanism
    > that is used to do this?


    There are two sides to every connection, so there are two IP addresses
    and TWO port numbers. The connection is identified by the tuple
    including both pairs of address:port endpoints.

    Thus, (10.0.0.1:12345,10.0.0.2:80) and (10.0.0.1:12346,10.0.0.2:80) are
    two separate connections between the same two machines with the same
    server port, but because the client port is different, the connections
    are distinct.
     
    Alan Krueger, Oct 19, 2005
    #7
  8. Guest

    Dear all
    Thanks to Gordon, Matt, Thomas, Alan, for taking time off to clarify
    what was (for me) a very vexing issue...the discussion has been most
    illuminating.

    Sierra Bravo
     
    , Oct 19, 2005
    #8
  9. Roedy Green Guest

    On 18 Oct 2005 02:19:50 -0700, wrote or quoted
    :

    >I understand that accept() returns a new Socket each time for every new
    >connection, and I assumed implicitly that each new Socket uses a new
    >(temporary) port.


    Nope.

    See http://mindprod.com/jgloss/tcpip.html

    There are four numbers that define a socket: client ip, client port,
    server ip, server port.

    The server can use the same ip/port for all conversations of a given
    type since the client ip/port distinguishes them.

    The only one who has a problem with ports is the client or client's
    firewall which has to assign unique free return ports.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 19, 2005
    #9
  10. Roedy Green Guest

    On 18 Oct 2005 04:10:50 -0700, "Sierra Bravo" <>
    wrote or quoted :

    >Interesting...I thought the idea of a port was to disambiguate between
    >multiple destinations for a packet in a single machine.


    it is, but many clients can share it.
    See http://mindprod.com/jgloss/port.html

    Please see http://mindprod.com/jgloss/tcpip.html

    It will should clear up your various misconceptions.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 19, 2005
    #10
  11. Roedy Green Guest

    On Tue, 18 Oct 2005 14:01:10 +0200, Thomas Weidenfeller
    <> wrote or quoted :

    >Not exactly. IP is not centered around hosts or nodes, but interfaces.
    >The host ID part of an IP address does not address that particular host
    >as a whole, but an IP interface at that host. A single machine can have
    >multiple interfaces, each with an own IP address.


    He means interface in the general sense, nothing to do with Java
    interafaces.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 19, 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. Ronnie
    Replies:
    1
    Views:
    540
    Roger Oshaug
    Dec 16, 2004
  2. -
    Replies:
    3
    Views:
    465
    Thomas Fritsch
    Jul 24, 2005
  3. Replies:
    2
    Views:
    6,312
  4. datbenik

    Simultaneous connections

    datbenik, Jan 20, 2006, in forum: Python
    Replies:
    2
    Views:
    280
    Diez B. Roggisch
    Jan 21, 2006
  5. mreister
    Replies:
    1
    Views:
    3,237
    mreister
    May 25, 2010
Loading...

Share This Page