2

I have looked up in BSD code but got lost somewhere :(

the reason I want to check is this:

TCP RFC (http://www.ietf.org/rfc/rfc793.txt) sec 2.7 states:

"To provide for unique addresses within each TCP, we concatenate an internet address identifying the TCP with a port identifier to create a socket which will be unique throughout all networks connected together. A connection is fully specified by the pair of sockets at the ends."

Does this mean: socket = local (ip + port) ?

If yes, then the accept function of Unix returns a new socket descriptor. Will it mean that a new socket is created (in turn a new port is created) for responding to client requests?

PS: I am a novice in network programming.

[UPDATE] I understood what I read @ How does the socket API accept() function work?. My only doubt is: if socket = (local port +local ip), then a new socket would mean a new port for the same IP. going by this logic, accept returns a new socket (thus a new port is created). so all sending should occur through this new port. Is what I understand here correct?

Community
  • 1
  • 1

1 Answers1

8

You are mostly correct. When you accept(), a new socket is created and the listening socket stays open to allow more incoming connections but the new socket uses the same local port number as the listening socket.

A connection is defined by a 5-tuple: protocol, local-addr, local-port, remote-addr, remote-port.

Therefore, each accepted connection is unique even though they all share the same local port number because the remote ip/port is always different. The listening socket has no remote ip/port and so is also unique.

Brian White
  • 8,332
  • 2
  • 43
  • 67
  • if indeed a socket is ip+port, then shouldn't a new socket have to have a new port associated with it ? (sry couldn't upvote ur ans coz of low reputation score...am a newbie) – user1801732 Nov 06 '12 at 04:55
  • 1
    @user1801732 that's the same as your original question, and the answer to that question is this answer. – hobbs Nov 06 '12 at 05:12
  • 2
    The accepted connection socket **must** have the same port number as the listening socket. If it changed, the client would have no way of knowing the new value and the initial TCP handshake would fail. – Brian White Nov 06 '12 at 14:49