Ports capable of multiple simultaneous connections?

S

sierra1bravo

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.
 
G

Gordon Beaton

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
 
S

Sierra Bravo

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
 
G

Gordon Beaton

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
 
M

Matt Humphrey

Sierra Bravo said:
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,
 
T

Thomas Weidenfeller

Sierra said:
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
 
A

Alan Krueger

Sierra said:
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.
 
S

sierra1bravo

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
 
R

Roedy Green

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.
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top