Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we have to get two file descriptors in TCP server socket programming?

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?

like image 866
TimeString Avatar asked Apr 19 '16 22:04

TimeString


People also ask

Why does TCP need 2 sockets?

The reason is that TCP has two different kinds of state that you want to control, whereas UDP has only one.

What is the role of file descriptors during socket creation?

The file descriptor acts as a pointer to the File Table which contains information about what action is to be taken i.e read, write, etc, and it contains pointers to the inode table of that particular file and as you might know inode contains all the necessary deatils of a file.

Can a file have multiple file descriptors?

One process can have multiple file descriptors point to the same entry (e.g., as a result of a call to dup() ) Multiple processes (e.g., a parent and child) can have file descriptors that point to the same entry.

What is file descriptor for TCP?

TCP provides a stream abstraction: Two processes, possibly on different machines, each have a file descriptor. Data written to either descriptor will be returned by a read from the other. Such network file descriptors are called sockets in Unix.

Do sockets have file descriptors?

Because each file or socket has a unique descriptor, the system knows exactly where to send and to receive the data.

Which are the two sockets with the TCP server process?

Typically, there are two types of applications that use TCP sockets - servers and clients. A TCP server listens on a well-known port (or IP address and port pair) and accepts connections from TCP clients. A TCP client initiates a connection request to a TCP server in order to setup a connection with the server.


3 Answers

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.

like image 82
David Schwartz Avatar answered Oct 24 '22 04:10

David Schwartz


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.

like image 20
fluter Avatar answered Oct 24 '22 02:10

fluter


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...

like image 36
Amadan Avatar answered Oct 24 '22 04:10

Amadan