Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Ports capable of multiple simultaneous connections?

Reply
Thread Tools

Ports capable of multiple simultaneous connections?

 
 
sierra1bravo@gmail.com
Guest
Posts: n/a
 
      10-18-2005
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.

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      10-18-2005
On 18 Oct 2005 02:19:50 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Sierra Bravo
Guest
Posts: n/a
 
      10-18-2005
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


 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      10-18-2005
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
 
Reply With Quote
 
Matt Humphrey
Guest
Posts: n/a
 
      10-18-2005

"Sierra Bravo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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,


 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      10-18-2005
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/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
Alan Krueger
Guest
Posts: n/a
 
      10-19-2005
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 addressort 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.
 
Reply With Quote
 
sierra1bravo@gmail.com
Guest
Posts: n/a
 
      10-19-2005
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

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-19-2005
On 18 Oct 2005 02:19:50 -0700, (E-Mail Removed) 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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-19-2005
On 18 Oct 2005 04:10:50 -0700, "Sierra Bravo" <(E-Mail Removed)>
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.
 
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
simultaneous copy to multiple media lannsjo@gmail.com Python 2 08-31-2006 05:11 PM
simultaneous copy to multiple media Swaroop C H Python 19 03-23-2005 04:01 PM
simultaneous multiple requests to very simple database Eric S. Johansson Python 10 01-24-2005 10:56 PM
Re: simultaneous multiple requests to very simple database Eric S. Johansson Python 2 01-19-2005 06:10 PM



Advertisments