0

I was trying to understand what does it mean that accept() creates a new socket and the accepted answer of this question was very helpful. My question comes from the comment section of the accepted answer as I believe it deserves to be a separate question. My understanding is this

  • (source ip, source port, destination ip, destination port, protocol) defines a socket.

  • A socket descriptor is an integer that works as an identifier of a socket (source ip, source port, destination ip, destination port, protocol)

Now apart from the question asked I also want to know whether my understanding "different socket descriptors returned by accept() and socket() points to the same socket (source ip, source port, destination ip, destination port, protocol) but represent different state of the same socket (listening state, connected to client state) just like same file can be in read mode/write mode" is correct or not.

Farsan Rashid
  • 1,460
  • 4
  • 17
  • 28

2 Answers2

2

The server's listening socket is listening on a given IP:port, but is not connected to anyone, so it is identifiable only by the listening IP:port.

When a client connection is established, accept() returns a new socket that is connected to a specific client, so it is identifiable by both server IP:port and client IP:port.

A socket represents a particular endpoint, which may be part of an underlying connection or not. A socket descriptor is just how the app refers to a given socket. A listening socket has 1 endpoint, whereas a connected socket has 2 endpoints. That is why accept() has to return a new socket that is different than the listening socket which accept() was called on.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I asked another question to narrow down the scope and feel like distinguishing socket and connected socket is the best way to go. https://stackoverflow.com/questions/50743329/what-does-it-mean-that-accept-creates-a-new-socket?noredirect=1#comment88500772_50743329 – Farsan Rashid Jun 07 '18 at 17:54
1

There isn't any. A listening socket is identified by a 2-tuple: IP address and port.

(source ip, source port, destination ip, destination port, protocol) defines a socket.

No. It defines a connection.

A socket descriptor is an integer that works as an identifier of a socket

Correct.

(source ip, source port, destination ip, destination port, protocol)

No, see above.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • So the listening socket is identifiable by two tuple and the other socket is by 4 tuple? (Ignoring protocol for both) – Farsan Rashid Jun 07 '18 at 09:33
  • Also if socket is identified by ip:port what is the significance of different socket descriptor returned by accept() and socket()? – Farsan Rashid Jun 07 '18 at 10:15
  • The significance of them being different is that they are different sockets. – user207421 Jun 07 '18 at 10:21
  • For both of them ip:port is same so how are they different can you please explain? @EJP – Farsan Rashid Jun 07 '18 at 10:24
  • 1
    The IP:port of a listening socket is the same as for *all* sockets accepted from it. That's how TCP works. – user207421 Jun 07 '18 at 10:27
  • 1
    @EJP Not quite. That is only true if the server is listening on a specific IP only. But if the server is listening on `0.0.0.0` or `::0` instead (to listen on all network interfaces), the server-side IP of an accepted socket will be the real IP of the specific interface that accepted the connection. – Remy Lebeau Jun 07 '18 at 17:49
  • @RemyLebeau Well, OK, but that's not right either. It is the IP address used by the client to connect, which may not be the IP address of the interface that accepted the connection, due to the weak end model. – user207421 Jun 07 '18 at 18:35