Question

Identify the correct order in which a server process must invoke the function calls accept, bind, listen, and recv according to UNIX socket API

Choose one option
Official answer and solution
Official answer
B
From
Official GATE 2015 answer key

Solution

Trace the lifecycle of a stream-oriented (TCP) server using the standard Berkeley UNIX sockets API.

A standard connection-oriented TCP server follows this execution sequence:

  1. socket(): Create the socket endpoint.
  2. bind(): Associate the socket with a local IP address and port number.
  3. listen(): Place the socket in passive listening mode with a designated backlog queue for incoming connections.
  4. accept(): Block until an incoming client connection request arrives, returning a new connected socket descriptor.
  5. recv(): Receive data from the connected client socket.

Thus, the four specified function calls must be invoked in the order:
bind →\rightarrow listen →\rightarrow accept →\rightarrow recv.

Final answer

B

Common trap

Calling listen() before bind(), or calling accept() before the socket has been put into listening state via listen().

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2015 Computer Science and Information Technology (CS) Session 2
Question
30 of 65, printed as CS Q.20
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.