6

I take this tutorial for server socket programming link. For the functionality, I have no problem about that, and what I'm asking is more about architecture design question. Please take a look in the tutorial. We actually see two file descriptors, one when calling socket(), and one when calling accept(). It makes sense why we get a file descriptor when creating a socket because we treat a socket as a file; it also makes sense that we have to have multiple file descriptors when accepting different connections. But why do we need to have both to make it work?

user207421
  • 305,947
  • 44
  • 307
  • 483
TimeString
  • 1,778
  • 14
  • 25
  • 1
    Not a great tutorial. `listen()` isn't error-checked. You can use `send()` and `recv()`. `For a server socket on the Internet, an address consists of a port number on the host machine` and for all other server sockets as well: the qualification is meaningless. `n` is ignored when printing the received data. The sockets are never closed. There are better tutorials out there. – user207421 Apr 19 '16 at 23:59

4 Answers4

6

The 1st socket is called the listening socket. TCP is a connection oriented stream. Each client connection operates on its own socket just like a file. If you only have one socket, you will not be able to distinguish which connection the data received on it belongs to. So the way TCP socket designed is to have the listening socket operate in LISTEN mode, and each time a client want to establish connection to the server, the accept call will return a new socket, aka the client socket, to represent the new connection, so that it is used to communication with this client exclusively.

On the other hand, UDP is a connectionless datagram-based protocol, in which just one socket is used to handle all data from all clients.

user207421
  • 305,947
  • 44
  • 307
  • 483
fluter
  • 13,238
  • 8
  • 62
  • 100
  • 1
    Just came across this post: http://stackoverflow.com/questions/20939193/is-new-socket-created-for-every-request, is it correct to say the first socket is a passive socket whereas the ones generated by accept() are called active sockets? – TimeString Apr 19 '16 at 23:11
  • 1
    @TimeString - passive is not an accurate word for it. Listening sockets are active sockets except there's no ESTABLISHED connection. Accepted sockets have ESTABLISHED connection but can be idle as long as they keep the TTL alive. – alvits Apr 19 '16 at 23:16
  • 1
    @alvits It is called a 'passive open' in RFC 793. 'Keep the TTL alive' is meaningless. – user207421 Apr 19 '16 at 23:50
5

One socket represents the listening endpoint. The other socket represents the accepted incoming connection. If you don't want to accept any more connections, you can close the listening socket after calling accept.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • So did I actually create two different (types of) sockets? – TimeString Apr 19 '16 at 22:56
  • 2
    Yes. They're both TCP endpoints, but one represents a listening endpoint bound only to a local address and the other represents an active TCP connection bound to both a local and a remote address. You can continue to `accept` more connections using the listening endpoint or you can `close` it if you don't need it. – David Schwartz Apr 19 '16 at 22:58
  • 1
    @TimeString - you can see both sockets by running `netstat -ant` and you'll notice there are actually 2 sockets. 1 is `ESTABLISHED` while the other is `LISTENING`. – alvits Apr 19 '16 at 23:16
  • @alvits, Thanks for providing a way to verify what's going on! – TimeString Apr 19 '16 at 23:20
3

Imagine a quick repair shop, where customers bring their PCs to be repaired, then sit in the waiting room till the PC is fixed. Not the long repairs that take weeks; the quick repairs that take an hour.

The sane way to run this shop is to have a receptionist who listens to the customers, takes the broken PC and passes it on to whichever repairman is currently free. He accepts the job, and goes off to work. The customer goes and sits. As another customer arrives, the receptionist is free to greet them, and directs them to another repairman if one is available.

The not sane way to run this shop is to have a receptionist who repairs the PC by themselves. The next customer to come to the shop has to hold their broken PC in their arms, waiting for the receptionist to repair the PC of the previous customer before they can hand over their load. Eventually, people's hands get tired, and they leave the queue, possibly to find a saner shop. Meanwhile, the poor receptionist is stressed, looking at all the people waiting to interact with her...

Amadan
  • 191,408
  • 23
  • 240
  • 301
1

Ideally, they are two different TCP endpoints, where one is used as listening endpoint(LISTENING) and the other one as the accepted incoming connection (ESTABLISTED). You can close the listening endpoint once you are done with accepting connections.

Karthik Balaguru
  • 7,424
  • 7
  • 48
  • 65