Reputation: 16428
I am creating a server socket as
% set serverSocket [socket -server accept 0]
sock005DBCC0
Since I am using the port number as zero, the operating system will allocate a free port to the server socket.
From the man page, I understand that we have to use -sockname
with chan configure
to get the port number.
-sockname
For client sockets (including the channels that get created when a client connects to a server socket) this option returns a list of three elements, the address, the host name and the port number for the socket. If the host name cannot be computed, the second element is identical to the address, the first element of the list.
% chan configure $serverSocket -sockname
0.0.0.0 0.0.0.0 65495 :: :: 65495
As you can see the above command returns six elements. What is the significance of ::
here ? Is it referring global scope ?
My actual intention is to get the port number to which the socket is listening.
So, in order to get the port number, can I retrieve the last element of the list alone as shown below?
% set serverPort [lindex [chan configure $serverSocket -sockname] end]
65495
%
The reason why I'm asking this is because of the repetition of that port number in that list.
Upvotes: 1
Views: 512
Reputation: 137557
There are two sets of three elements in that list precisely because the server socket under the covers is actually two: one for IPv4 and one for IPv6. Tcl opens both since it doesn't know how clients are going to connect ahead of time (unless you use the -myaddr
option when making the server option to make it so that only one protocol is possible). They could theoretically be on different ports, but that's really quite unlikely as Tcl tries to use the same port for both; your idea about taking the last item is probably fine.
If you really care, when you've got two addresses the first one will be the IPv4 address and the second will be the IPv6 address, so you can use lindex … 2
or lindex … 5
(or lindex … end
) to pick exactly what you mean.
I'd probably do:
lassign [chan configure $serverSocket -sockname] serverAddress serverName serverPort
Upvotes: 1
Reputation: 7034
::
is the equivalent of 0.0.0.0
, the unspecified address, for IPv6
(http://www.ietf.org/rfc/rfc3513.txt, page)
It looks like the port is 65495. You're getting two sets of three elements, one for ipv4, one for ipv6
Upvotes: 1