|
Home > Archive > Unix Programming > January 2004 > Newbie TCP/IP Socket Question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Newbie TCP/IP Socket Question
|
|
| Nimmi Srivastav 2004-01-23, 5:02 pm |
| I know that in TCP sockets, when the server accepts a connection from
the client, the server creates a new socket descriptor. This new
socket descriptor is then used in subsequent communication with the
client. My question is this:
What is the port number that the server uses for subsequent
communication with the client?
According to Unix Network programming by Stevens (old edition ----
i.e. the single volume book), accept() creates a new socket with the
"same properties" as the original socket. To me the phrase "same
properties" implies that the port number is the same in both the
cases. In other words, if the server is waiting to accept new
connections on port number 12345, after the new socket has been
established, does the server still use port number 12345?
Thanks,
NS
| |
| =?iso-8859-1?q?M=E5ns_Rullg=E5rd?= 2004-01-23, 5:02 pm |
| nimmi_srivastav@yahoo.com (Nimmi Srivastav) writes:
quote:
> I know that in TCP sockets, when the server accepts a connection from
> the client, the server creates a new socket descriptor. This new
> socket descriptor is then used in subsequent communication with the
> client. My question is this:
>
> What is the port number that the server uses for subsequent
> communication with the client?
>
> According to Unix Network programming by Stevens (old edition ----
> i.e. the single volume book), accept() creates a new socket with the
> "same properties" as the original socket. To me the phrase "same
> properties" implies that the port number is the same in both the
> cases. In other words, if the server is waiting to accept new
> connections on port number 12345, after the new socket has been
> established, does the server still use port number 12345?
Yes. Both the old and the new socket use port 12345. The server
socket will only accept packets with the SYN flag set. Those are
packets sent by the connect() call to open a new connection. For
other packets, their TCP session number determines which socket they
belong to.
--
Måns Rullgård
mru@kth.se
| |
| =?iso-8859-1?q?M=E5ns_Rullg=E5rd?= 2004-01-23, 5:02 pm |
| nimmi_srivastav@yahoo.com (Nimmi Srivastav) writes:
quote:
> I know that in TCP sockets, when the server accepts a connection from
> the client, the server creates a new socket descriptor. This new
> socket descriptor is then used in subsequent communication with the
> client. My question is this:
>
> What is the port number that the server uses for subsequent
> communication with the client?
>
> According to Unix Network programming by Stevens (old edition ----
> i.e. the single volume book), accept() creates a new socket with the
> "same properties" as the original socket. To me the phrase "same
> properties" implies that the port number is the same in both the
> cases. In other words, if the server is waiting to accept new
> connections on port number 12345, after the new socket has been
> established, does the server still use port number 12345?
Yes. Both the old and the new socket use port 12345. The server
socket will only accept packets with the SYN flag set. Those are
packets sent by the connect() call to open a new connection. For
other packets, their TCP session number determines which socket they
belong to.
--
Måns Rullgård
mru@kth.se
| |
| Markus Zingg 2004-01-23, 5:02 pm |
| >I know that in TCP sockets, when the server accepts a connection fromquote:
>the client, the server creates a new socket descriptor. This new
>socket descriptor is then used in subsequent communication with the
>client. My question is this:
>
> What is the port number that the server uses for subsequent
> communication with the client?
>
>According to Unix Network programming by Stevens (old edition ----
>i.e. the single volume book), accept() creates a new socket with the
>"same properties" as the original socket. To me the phrase "same
>properties" implies that the port number is the same in both the
>cases. In other words, if the server is waiting to accept new
>connections on port number 12345, after the new socket has been
>established, does the server still use port number 12345?
Remembers me of the good old days. I used the same book, and also
stumbled over the same question. :-)
What you must understand is that a socket connection is ALWAYS defined
out of FOUR informations
a) the remote IP address
b) the remote Port number
c) the local IP address
d) the local port number (12345)
So, in the above example, if the remote host would connect to the
local host & port twice, the local end of the two sockets would be
identical but not the remote part! Therefore, those two connections
would be clearly distinguished by the differing remote port number and
hence the comunication is uniquely defined.
With regard to the original socket in unconnected state (during the
accept() call) the local end would also be identical to the above two
sockets, but the remote part would remain undefined to allow further
connections. Wether such a socket really exists or not is depending on
the given TCP implementation.
HTH
Markus
| |
| Markus Zingg 2004-01-23, 5:02 pm |
| >I know that in TCP sockets, when the server accepts a connection fromquote:
>the client, the server creates a new socket descriptor. This new
>socket descriptor is then used in subsequent communication with the
>client. My question is this:
>
> What is the port number that the server uses for subsequent
> communication with the client?
>
>According to Unix Network programming by Stevens (old edition ----
>i.e. the single volume book), accept() creates a new socket with the
>"same properties" as the original socket. To me the phrase "same
>properties" implies that the port number is the same in both the
>cases. In other words, if the server is waiting to accept new
>connections on port number 12345, after the new socket has been
>established, does the server still use port number 12345?
Remembers me of the good old days. I used the same book, and also
stumbled over the same question. :-)
What you must understand is that a socket connection is ALWAYS defined
out of FOUR informations
a) the remote IP address
b) the remote Port number
c) the local IP address
d) the local port number (12345)
So, in the above example, if the remote host would connect to the
local host & port twice, the local end of the two sockets would be
identical but not the remote part! Therefore, those two connections
would be clearly distinguished by the differing remote port number and
hence the comunication is uniquely defined.
With regard to the original socket in unconnected state (during the
accept() call) the local end would also be identical to the above two
sockets, but the remote part would remain undefined to allow further
connections. Wether such a socket really exists or not is depending on
the given TCP implementation.
HTH
Markus
| |
| David Schwartz 2004-01-23, 5:02 pm |
|
"Nimmi Srivastav" <nimmi_srivastav@yahoo.com> wrote in message
news:8b0c42d.0312021331.29af3b68@posting.google.com...
quote:
> I know that in TCP sockets, when the server accepts a connection from
> the client, the server creates a new socket descriptor. This new
> socket descriptor is then used in subsequent communication with the
> client. My question is this:
>
> What is the port number that the server uses for subsequent
> communication with the client?
The server's source port is the same as the port the client connected
to. The difference between the socket you get back from 'accept' and the
listening socket is that the listening socket has no destination port. The
socket you get back from 'accept' uniquely identifies the TCP connection to
that particular client IP and port.
A web server will typically listen on port 80. A client might then
connect from port 5043 to port 80 on the web server. Each established TCP
connection is identified by its source IP, source port, destination IP, and
destination port. All the TCP connections to the same web server will have
the same source port and usually the same source IP (as seen from the
server) but no two will have both the same destination IP and the same
destination port.
quote:
> According to Unix Network programming by Stevens (old edition ----
> i.e. the single volume book), accept() creates a new socket with the
> "same properties" as the original socket. To me the phrase "same
> properties" implies that the port number is the same in both the
> cases. In other words, if the server is waiting to accept new
> connections on port number 12345, after the new socket has been
> established, does the server still use port number 12345?
I don't think that's what Stevens was trying to say. I think he was
referring to socket options and properties of the sockets interface itself.
However your conclusion is correct, all TCP connections accepted on the same
listening port will have the same destination port (as seen by the server)
and may have the same IP (though they won't all if the server has more than
one IP address).
DS
| |
| David Schwartz 2004-01-23, 5:02 pm |
|
"Nimmi Srivastav" <nimmi_srivastav@yahoo.com> wrote in message
news:8b0c42d.0312021331.29af3b68@posting.google.com...
quote:
> I know that in TCP sockets, when the server accepts a connection from
> the client, the server creates a new socket descriptor. This new
> socket descriptor is then used in subsequent communication with the
> client. My question is this:
>
> What is the port number that the server uses for subsequent
> communication with the client?
The server's source port is the same as the port the client connected
to. The difference between the socket you get back from 'accept' and the
listening socket is that the listening socket has no destination port. The
socket you get back from 'accept' uniquely identifies the TCP connection to
that particular client IP and port.
A web server will typically listen on port 80. A client might then
connect from port 5043 to port 80 on the web server. Each established TCP
connection is identified by its source IP, source port, destination IP, and
destination port. All the TCP connections to the same web server will have
the same source port and usually the same source IP (as seen from the
server) but no two will have both the same destination IP and the same
destination port.
quote:
> According to Unix Network programming by Stevens (old edition ----
> i.e. the single volume book), accept() creates a new socket with the
> "same properties" as the original socket. To me the phrase "same
> properties" implies that the port number is the same in both the
> cases. In other words, if the server is waiting to accept new
> connections on port number 12345, after the new socket has been
> established, does the server still use port number 12345?
I don't think that's what Stevens was trying to say. I think he was
referring to socket options and properties of the sockets interface itself.
However your conclusion is correct, all TCP connections accepted on the same
listening port will have the same destination port (as seen by the server)
and may have the same IP (though they won't all if the server has more than
one IP address).
DS
| |
| briggs@encompasserve.org 2004-01-23, 5:02 pm |
| In article <yw1xfzg2lx6f.fsf@kth.se>, mru@kth.se (=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=) writes:quote:
> nimmi_srivastav@yahoo.com (Nimmi Srivastav) writes:
>
>
> Yes. Both the old and the new socket use port 12345. The server
> socket will only accept packets with the SYN flag set. Those are
> packets sent by the connect() call to open a new connection. For
> other packets, their TCP session number determines which socket they
> belong to.
TCP session number? There is no such field in a TCP packet.
The source port number (taken together with the destination port
number, source IP address and destination IP address) determine
which socket should be used.
John Briggs
| |
| =?iso-8859-1?q?M=E5ns_Rullg=E5rd?= 2004-01-23, 5:02 pm |
| briggs@encompasserve.org writes:
quote:
>
> TCP session number? There is no such field in a TCP packet.
Yes, of course. I had mixed things up with the sequence number. It's
been a while since I looked at the inner workings of TCP. Sorry for
the confusion.
quote:
> The source port number (taken together with the destination port
> number, source IP address and destination IP address) determine
> which socket should be used.
>
> John Briggs
--
Måns Rullgård
mru@kth.se
| |
| briggs@encompasserve.org 2004-01-23, 5:02 pm |
| In article <yw1xfzg2lx6f.fsf@kth.se>, mru@kth.se (=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=) writes:quote:
> nimmi_srivastav@yahoo.com (Nimmi Srivastav) writes:
>
>
> Yes. Both the old and the new socket use port 12345. The server
> socket will only accept packets with the SYN flag set. Those are
> packets sent by the connect() call to open a new connection. For
> other packets, their TCP session number determines which socket they
> belong to.
TCP session number? There is no such field in a TCP packet.
The source port number (taken together with the destination port
number, source IP address and destination IP address) determine
which socket should be used.
John Briggs
| |
| =?iso-8859-1?q?M=E5ns_Rullg=E5rd?= 2004-01-23, 5:02 pm |
| briggs@encompasserve.org writes:
quote:
>
> TCP session number? There is no such field in a TCP packet.
Yes, of course. I had mixed things up with the sequence number. It's
been a while since I looked at the inner workings of TCP. Sorry for
the confusion.
quote:
> The source port number (taken together with the destination port
> number, source IP address and destination IP address) determine
> which socket should be used.
>
> John Briggs
--
Måns Rullgård
mru@kth.se
| |
| Jon Anderson 2004-01-23, 5:02 pm |
|
To me the phrase "samequote:
> properties" implies that the port number is the same in both the
> cases. In other words, if the server is waiting to accept new
> connections on port number 12345, after the new socket has been
> established, does the server still use port number 12345?
>
Yes. The after the acept() call the original fd and address
binding remains in listen state. The new established connection
is handled on the new descriptor and identified by the client/
server address tuple (which must be unique).
quote:
> Thanks,
> NS
--
Jon Anderson
Senior Support Engineer (PTS-EMEA-NETWORK)
Sun Microsystems Inc.
Tel: ++44 (0)1252 421 868
| |
| Jon Anderson 2004-01-23, 5:02 pm |
|
To me the phrase "samequote:
> properties" implies that the port number is the same in both the
> cases. In other words, if the server is waiting to accept new
> connections on port number 12345, after the new socket has been
> established, does the server still use port number 12345?
>
Yes. The after the acept() call the original fd and address
binding remains in listen state. The new established connection
is handled on the new descriptor and identified by the client/
server address tuple (which must be unique).
quote:
> Thanks,
> NS
--
Jon Anderson
Senior Support Engineer (PTS-EMEA-NETWORK)
Sun Microsystems Inc.
Tel: ++44 (0)1252 421 868
|
|
|
|
|